javascript - 得到一个 mysql 语法错误但看不到问题
问题描述
我刚刚开始使用 React Native、Typescript、Node 和 MySQL 创建一个健身房跟踪应用程序来改进我的后端(后端是指我在后端开发方面的熟练程度,我不是希望创建应用程序来跟踪我的深蹲改进我的后端,虽然它可以做一些工作。此外,健身房跟踪应用程序的意思本质上是一个健身房跟踪记事本的替代品,并添加了一些图表等。)
我创建了一条路线,可以发布用户的最新集合,其中包含所有常见的信息,例如体重、次数等。
这是发布到数据库的函数:
export const addLogEntry = (req: Request, res: Response) => {
fetch('http://localhost:3000/logEntries', {
method: 'POST',
body: JSON.stringify({ id: req.body.id }),
headers: { 'Content-Type': 'application/json' },
})
.then((response: any) => response.json())
.then((data: any) => {
req.body.entry.id = data.length + 1;
req.body.entry.userId = req.body.id;
const {
id,
set,
logId,
weight,
reps,
time,
rest,
exerciseId,
userId,
} = req.body.entry;
console.log(id, set, logId, weight, reps, time, rest, exerciseId, userId);
connection.query(
`INSERT into logEntries(id, set, logId, weight, reps, time, rest, exerciseId, userId) VALUES(${id}, ${set}, ${logId}, ${weight}, ${reps}, ${time}, ${rest}, ${exerciseId}, ${userId})`,
function (error, results, fields) {
if (error) throw error;
res.send('');
}
);
})
.catch((error) => {
throw error;
});
};
这是请求的正文:
{
"id": 1,
"entry": {
"set": 2,
"logId": 2,
"weight": 82.5,
"reps": 12,
"time": 0,
"rest": 60,
"exerciseId": 1
}
}
这是表结构的屏幕截图:
这是错误:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set, logId, weight, reps, time, rest, exerciseId, userId) VALUES(9, 2, 2, 82.5, ' at line 1
有人有什么主意吗?我觉得这是一个语法错误,这意味着它很可能正盯着我看。
解决方案
推荐阅读
- javascript - 将 Dynamic Json 结构修改为平面键和值
- python - Python Websockets 使用队列将数据从客户端处理程序传递到协程进行处理
- git - Git Rebase 从带有 Squashed 合并的分支
- javascript - 基于位置的跟踪的 ar.js/Aframe 多个对象无法正常工作
- salesforce - 不仅将组件用于帐户所有者,而且通常用于记录所有者
- javascript - 将迭代中的值复制到 salesforce aura 中的另一个字段的文本
- wordpress - 对于本机反应,有没有办法只反应 json 文件中的某些数据并渲染它?
- c++ - MacOS Monterey 中的堆内存分配
- r - 是否可以将列表通过管道传输到 ggsave
- python - 在 python (turtle) 上创建屏幕