javascript - 即使用户存在于 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');
}
});
}
解决方案
问题在于您不应该:
在客户端使用的路线,应该是
'/users/checkUserInDatabase/'+ x
和服务器端应该是
'/checkUserInDatabase/:email'
推荐阅读
- multidimensional-array - Octave:如何为现有单元格数组分配正确的维度以避免“错误:=:不一致的参数(op1 是 2x2x2,op2 是 2x2x2)”?
- java - 如何修复错误:java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server.)?
- xcode - 在 VStack 内部的 HStack 内的 SwiftUI 文本字段中,在 macOS 中生成运行时错误
- r - 读取多个文件,但跟踪哪个文件是 R 中的哪个数据框
- python - 如何消除“pip3 install conda”的影响?
- node.js - 我从官方网站下载了 NodeJS,但它在 Ubuntu 上不起作用
- rust - 当 Rust 中的函数需要可变引用时,为什么不可变引用可以匹配工作?
- python - 选择多个唯一索引列对形成数据框
- alert - 如何根据动态标签的结果在 pine 脚本中创建警报?
- sql - SQL 查询,特定字段上的一条记录,带有 group by 子句