首页 > 解决方案 > 即使用户存在于 nodejs 应用程序的 mongodb 中,model.findOne() 也会返回 null

问题描述

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

}

标签: javascriptnode.jsmongodbmongoose

解决方案


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

'/users/checkUserInDatabase/'+ x 

和服务器端应该是

'/checkUserInDatabase/:email'

推荐阅读