findOne返回空始终为
我在数据库中有“用户(hello@gmail.com)”数据,但是User.findOne总是返回null。请帮帮我。我在后台运行了一个节点,但它给了我null。req.body给出了我经过但findOne没有工作的电子邮件。User.create工作得很好。但是像find,findById,findOne这样的搜索查询不起作用。SomeOne,请帮帮我。
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
}
})
}发布于 2021-05-03 14:19:29
文档应该将active: true保存在数据库中。但是,相反,我将每个文档保存为默认值一样的假。所以,这是我的答案,如果将来有人觉得有用的话,请告诉我。保重再见
发布于 2021-04-20 15:42:58
我觉得你的问题就在你考试的前两行?
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(),but it execute the query and return a Promise...
https://stackoverflow.com/questions/67180337
复制相似问题