正确的身份验证nodejs express mysql

dw1jzc5e  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(287)

所以我有这个问题,我有点搞不懂逻辑是怎么工作的。我正在创建一个新的用户注册方法,里面有一个检查用户是否使用同一封电子邮件的方法。我用 Postman 当张贴查询。
第一个查询工作正常,包括电子邮件检查方法和注册新邮件,而对于第二个查询,电子邮件检查工作,但注册新邮件不工作, Postman 一直加载,直到一分钟左右,直到它说'无法得到答复'。
第一个(作品)

exports.postContact = router.post("/api/users/new", (req, res) => {
  var today = new Date();
  var users = {
    "first_name": req.body.first_name,
    "last_name": req.body.last_name,
    "email": req.body.email,
    "password": req.body.password,
    "created": today,
    "modified": today
  }

  var email = req.body.email;

  mySQL.query('SELECT * FROM users WHERE email = ?', [email], function (err, results, fields) {
    if (results.length > 0) {
      if (results[0].email == email) {
        res.send({
          "code": 204,
          "status": 'Failed',
          "message": "This email address is already registered, please login instead"
        });
      }
    }
    else {
      mySQL.query("INSERT INTO users SET ?", users,
        (err, results, fields) => {
          if (err)
            res.send({
              "code": 400,
              "status": 'Failed',
              "message": 'Something wrong with the connection'
            })
          res.send({
            "code": 400,
            "status": 'Success',
            "message": 'User is successfully registered'
          })
        }
      );
    }
  });
});

第二个(不起作用)

exports.postContact = router.post("/api/users/new", (req, res) => {
  var today = new Date();
  var users = {
    "first_name": req.body.first_name,
    "last_name": req.body.last_name,
    "email": req.body.email,
    "password": req.body.password,
    "created": today,
    "modified": today
  }

  var email = req.body.email;

  mySQL.query('SELECT * FROM users WHERE email = ?', [email], function (err, results, fields) {
    if (results.length > 0) {
      if (results[0].email == email) {
        res.send({
          "code": 204,
          "status": 'Failed',
          "message": "This email address is already registered, please login instead"
        });
      }
      else {
        mySQL.query("INSERT INTO users SET ?", users,
          (err, results, fields) => {
            if (err)
              res.send({
                "code": 400,
                "status": 'Failed',
                "message": 'Something wrong with the connection'
              })
            res.send({
              "code": 400,
              "status": 'Success',
              "message": 'User is successfully registered'
            })
          }
        );
      }
    }
  });
});
vpfxa7rd

vpfxa7rd1#

可能需要刷新sql语句。
我相信这是正确的:

INSERT INTO users SET ?

mysql的正确插入查询:

INSERT INTO table
  (column1, column2, ... )
VALUES
  (expression1, expression2, ... ),
  (expression1, expression2, ... )

这里有一个链接供参考。

相关问题