Javascript MongoDB User.findOne({email:email})
findOne始终返回null 我有一个“用户”(hello@gmail.com)“数据库中的数据,但User.findOne始终返回null。请帮帮我。我有一个在后台运行的节点,但它给我空值req.body给出我正在传递但findOne不起作用的电子邮件User.create工作正常。但是像find、findById、findOne这样的搜索查询不起作用。有人请帮帮我Javascript MongoDB User.findOne({email:email}),javascript,jquery,node.js,mongodb,mongoose,Javascript,Jquery,Node.js,Mongodb,Mongoose,findOne始终返回null 我有一个“用户”(hello@gmail.com)“数据库中的数据,但User.findOne始终返回null。请帮帮我。我有一个在后台运行的节点,但它给我空值req.body给出我正在传递但findOne不起作用的电子邮件User.create工作正常。但是像find、findById、findOne这样的搜索查询不起作用。有人请帮帮我 const userSchema = new mongoose.Schema({ name: { type: Str
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'A user must have a unique name'],
unique: true,
},
password: {
type: String,
required: true,
unique: true,
minlength: 8,
},
email: {
type: String,
required: [true, 'A user must have a unique name'],
unique: true,
lowercase: true,
}
const User = mongoose.model("User", userSchema);
exports.getTest=async (req,res)=>{
const {email} = req.body // returns "hello@gmail.com"
const user = await User.findOne({email})
console.log(user) //returns null
res.json({
status:"success",
data:{
user
}
})
}
我觉得你的问题在于考试的前两行
const {email} = req.body // returns "hello@gmail.com"
const user = await User.findOne({email})
如果您的req.body
只是返回电子邮件字符串,那么您的查询可能不是正确的JSON格式
您确定查询实际上是User.findOne({email:hello@gmail.com“})
而不仅仅是User.findOne(”hello@gmail.com”
此外,还不确定是否需要
exec()
,最后,文档应将active:true
保存在数据库中。但是,我将每个文档保存为默认值false。所以,这是我的答案,如果将来有人觉得它有用,请告诉我。小心,再见您的电子邮件字段可能有空格或不匹配。您的语法看起来正确,不需要exec()。三次检查所有假设,因为它们是所有邪恶的根源。有人告诉我,.exec()
使Mongoose请求异步,我永远无法检查这是不是真的这是我第二周试图找到我的bug。我找不到。这就是为什么我需要你们的帮助。User.findOne({email:hello@gmail.com“})
是的,先生。我检查了一百万次。这些问题不起作用我不知道为什么