How to use mongoose findOne

前端 未结 5 1673
死守一世寂寞
死守一世寂寞 2020-12-01 07:32

I have the below schema (apologies that it is in coffeescript)

Schema = mongoose.Schema

AuthS = new Schema
    auth:   {type: String, unique: true}
    nick         


        
5条回答
  •  情书的邮戳
    2020-12-01 07:48

    In my case same error is there , I am using Asyanc / Await functions , for this needs to add AWAIT for findOne

    Ex:const foundUser = User.findOne ({ "email" : req.body.email });
    

    above , foundUser always contains Object value in both cases either user found or not because it's returning values before finishing findOne .

    const foundUser = await User.findOne ({ "email" : req.body.email });
    

    above , foundUser returns null if user is not there in collection with provided condition . If user found returns user document.

提交回复
热议问题