无法使用find查询在mongoose中查找数据
在我的应用程序中,我希望用户input用户名和密码,然后用户名首先在数据库中使用查询查询进行检查,如果用户名发现则将新页面呈现给用户。 问题是如果我input一个错误的用户名和密码,那么它也呈现一个新的页面,并且页面也不完全加载。 请告诉我什么是错误的。
在app.js我写了:
app.post('/profile',users.login);
在users.js我写:
exports.login=function(req,res){ console.log("login called"); PersonalInfo.findOne({ name:req.params.userName}, function(err,data){ if(err) { console.log("find is not done"); console.log(data); } else{ res.render("/profilearea.ejs"); } })
}
当findOne
查询找不到匹配的文档时,不会将其视为错误。 相反,“未find”的情况是由data
参数指示findOne
callback为null
。
你的findOne
callback函数的代码应该如下所示:
if(err || !data) { console.log("find is not done"); } else{ res.render("/profilearea.ejs"); }