首页 > 解决方案 > 嵌套查询在 Node.js 和 MySQL 中不起作用

问题描述

我的代码如下。

我得到的错误是在包含 UPDATE 语句的查询语句中。

为什么我得到一个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 'WHERE email = 'test@gmail.com'' at line 1

控制器

...

exports.login = (req, res, next) => {
  const { email, password } = req.body;
  const secretKey = req.app.get('jwt-secret');

  let sql = `SELECT * FROM user WHERE email = ?`;

  User.query(sql, [email], (err, result) => {
    ...
    if (result[0] === undefined || result[0].password !== encrypted) {
      res.status(300).send('ID, PS check plz');
    } else {
      const token = jwtSign(secretKey, result[0].userId, email);

      sql = `UPDATE user SET token = ? WHERE email = ?`;

      User.query(sql, [token, email], (err, result) => {
        console.log(err);
        if (err) {
          return next(err);
        }
        ...
      });
    }
  });
};

标签: javascriptmysqlnode.js

解决方案


推荐阅读