当用户在表单中输入时,需要检查mysql中是否存在电子邮件。 从表单requser.email将在表单中输入用户emaild。如何将requser.email与数据库进行比较以找到匹配。
router.post('/signup', function(req, res, next) {
var requser = req.body.data.user;
var hash = bcrypt.hashSync(req.body.data.user.password)
var str = "";
/*email check start*/
/*email check end*/
switch (requser.role) {
case "approver":
str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","approver")';
break;
case "user":
str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","user")';
break;
case "admin":
str = 'INSERT INTO users (name, email, password,role) values("' + requser.name + '","' + requser.email + '","' + hash + '","admin")';
break;
}
if (!str) {
return res.json({
"status": "error",
"data": {
"redirect": "/"
}
});
}
connection.query(str, function(error, rows) {
if (error) {
console.log("error ocurred", error);
}
//console.log(rows);
res.json({
"status": error ? "error" : "success",
"data": rows || ""
})
});
});
答案 0 :(得分:1)
您可以编写SELECT 1
查询以获取包含该电子邮件地址的记录,也可以在电子邮件字段中定义UNIQUE CONSTRAINT
,以便插入失败。
Here's UNIQUE CONSTRAINT
的示例,以及SELECT 1
的示例:
SELECT 1 FROM users WHERE email = <email>;