Javascript 节点js MongoDB登录系统数据库值未定义
我目前正在使用Nodejs、Express和MongoDB开发一个登录系统。除了数据库中的值是未定义的之外,其他一切都正常工作。在两个console.log语句中,声明了“database __;”,结果是未定义的。不太清楚为什么,从一些测试来看,用户输入的值工作正常,所以我不知道为什么返回未定义Javascript 节点js MongoDB登录系统数据库值未定义,javascript,node.js,database,mongodb,authentication,Javascript,Node.js,Database,Mongodb,Authentication,我目前正在使用Nodejs、Express和MongoDB开发一个登录系统。除了数据库中的值是未定义的之外,其他一切都正常工作。在两个console.log语句中,声明了“database __;”,结果是未定义的。不太清楚为什么,从一些测试来看,用户输入的值工作正常,所以我不知道为什么返回未定义 app.post("/login", (req, res) => { //Get user fields const userEmail = req.body.log
app.post("/login", (req, res) => {
//Get user fields
const userEmail = req.body.loginEmail;
const userPass = req.body.loginPassword;
//Is user in database?
User.find({ email: userEmail }, (err, user) => {
console.log("database email: " + user.email)
if (!err) {
//Compare password to database password
bcrypt.compare(userPass, user.password, (err, result) => {
console.log("database password: " + user.password);
//If user pass in database, check if verified & redirect to success
if (userPass === user.password) {
if (user.isVerified) {
res.redirect("/success");
} else {
res.send(
"You are not verified. Please check your email to access your account."
);
}
} else {
res.send("Incorrect password");
}
});
} else {
res.send(err);
}
});
});
当您使用时,Mongoose将返回一个数组作为回调函数的第二个参数。如果您使用单个文档,则将返回。此部分用户。查找({email:userEmail},(err,User)=>{指的是Mongoose库?是的,我使用的是Mongoose。