首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB User.findOne({email:email})

MongoDB User.findOne({email:email})
EN

Stack Overflow用户
提问于 2021-04-20 13:52:41
回答 2查看 1.2K关注 0票数 1

findOne返回空始终为

我在数据库中有“用户(hello@gmail.com)”数据,但是User.findOne总是返回null。请帮帮我。我在后台运行了一个节点,但它给了我null。req.body给出了我经过但findOne没有工作的电子邮件。User.create工作得很好。但是像find,findById,findOne这样的搜索查询不起作用。SomeOne,请帮帮我。

代码语言:javascript
复制
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
        }
    })

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-03 14:19:29

文档应该将active: true保存在数据库中。但是,相反,我将每个文档保存为默认值一样的假。所以,这是我的答案,如果将来有人觉得有用的话,请告诉我。保重再见

票数 0
EN

Stack Overflow用户

发布于 2021-04-20 15:42:58

我觉得你的问题就在你考试的前两行?

代码语言:javascript
复制
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...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67180337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档