即使用户存在于nodejs应用程序的mongodb中,model.findOne()仍返回null

时间:2019-04-12 19:54:55

标签: javascript node.js mongodb mongoose

我正在尝试检查用户是否已存在于数据库中。我正在使用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');
   }
 });

}

2 个答案:

答案 0 :(得分:1)

问题是您不应在客户端使用:的路由,应该是

'/users/checkUserInDatabase/'+ x 

服务器端应为

'/checkUserInDatabase/:email'

答案 1 :(得分:0)

似乎您需要在console.log之后res.send(JSON.stringify(user));