首页 > 解决方案 > 得到一个 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
    }
}

这是表结构的屏幕截图:

logEntries 表结构

这是错误:

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

有人有什么主意吗?我觉得这是一个语法错误,这意味着它很可能正盯着我看。

标签: javascriptmysqlreact-native

解决方案


推荐阅读