【发布时间】:2020-11-29 10:50:10
【问题描述】:
这是我的代码。
router.route("/login").post((req, res) => {
const email = req.body.email;
const password = req.body.password;
Account.find().then((account) => {
account.forEach((eachAcc) => {
if (eachAcc.email.toUpperCase() === email.toUpperCase()) {
bcrypt.compare(password, eachAcc.password, function (err, result) {
if (result == true) res.status(200).json("Login Successful");
else if (err) res.status(400).json("Error: " + err);
});
}
});
res.status(400).json("Invalid email or password");
});
});
但我总是收到无效的电子邮件/密码,我希望仅在循环完成且电子邮件/密码不匹配时打印它。
请帮忙。
【问题讨论】:
-
只有在至少有一个错误 400 状态时才应该显示无效的电子邮件/密码?
-
避免使用
if (result == true),要么使用if (result),要么使用if (result === true)。 stackoverflow.com/a/15394130/6451036 -
你在这里使用
Account.find()的目的是什么?您需要一个函数作为参数来测试一个值。根据我的理解,不带参数调用它不会返回任何内容。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
@Sascha 是的错误消息应该显示至少一个错误
-
@Aroic here Accound.find() 是一个猫鼬查询,用于查找我的数据库中的所有帐户。 mongoosejs.com/docs/api.html#model_Model.find
标签: javascript node.js