即使用户存在于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');

   }

 });


}


月关宝盒
浏览 142回答 2
2回答

慕婉清6462132

问题出在您不应该:在客户端使用的路由中,应该是'/users/checkUserInDatabase/'+ x和服务器端应该是'/checkUserInDatabase/:email'

开心每一天1111

看起来您需要立即访问res.send(JSON.stringify(user));console.log。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript