gpt4 book ai didi

mysql - 正确的身份验证 NodeJS Express MySQL

转载 作者:行者123 更新时间:2023-11-29 16:49:45 27 4
gpt4 key购买 nike

所以我有这个疑问,我对逻辑如何工作有点困惑。我正在创建一个新的用户注册方法,其中有一个检查用户是否使用相同电子邮件的方法。我在发布查询时使用 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'
})
}
);
}
}
});
});

最佳答案

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

INSERT INTO users SET ?

MySQL 的正确插入查询:

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

Here's a link for reference.

关于mysql - 正确的身份验证 NodeJS Express MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52870786/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com