[英]model.findOne() returns null even though user exists in the mongodb in nodejs app
我正在尝试检查用户是否已经存在于数据库中。 我正在使用javascript函数从客户端发送用户的电子邮件ID到节点js路由。 路由应查找用户是否存在于数据库中。 但是所有结果都为空。
/* this is the route*/
router.route('/checkUserInDatabase:email')
.get(async(req,res)=>{
const user = await User.findOne({ 'email': req.params.email });
console.log(user);
});
/* this is the client side function*/
function checkifEmailExists(x){
$.get('/users/checkUserInDatabase:'+ x , function(data, status){
if(data=='true'){
alert('email exists');
}
if(data=='false'){
alert('email does not exist');
}
});
}
问题在于您不应该使用的路由:
在客户端,应该是
'/users/checkUserInDatabase/'+ x
和服务器端应该是
'/checkUserInDatabase/:email'
看起来您需要重新发送res.send(JSON.stringify(user));
在您的console.log之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.