Set Variable to result of Mongoose Find

后端 未结 3 686
余生分开走
余生分开走 2021-01-01 00:37

I\'m trying to do something like this

    function retrieveUser(uname) {
      var user = User.find({uname: uname}, function(err, users) {
        if(err)
           


        
3条回答
  •  再見小時候
    2021-01-01 01:40


    Updated on 25th Sept. 2019

    Promise chaining can also be used for better readability:

    Model
    .findOne({})
    .exec()
    .then((result) => {
       // ... rest of the code
       return Model2.findOne({}).exec();
    })
    .then((resultOfModel2FindOne) => {
       // ... rest of the code
    })
    .catch((error) => {
       // ... error handling
    });
    

    I was looking for an answer to the same question.

    Hopefully, MongooseJS has released v5.1.4 as of now.

    Model.find({property: value}).exec() returns a promise.

    it will resolve to an object if you use it in the following manner:

    const findObject = (value) => {
      return Model.find({property: value}).exec();
    }
    
    mainFunction = async => {
         const object = await findObject(value);
         console.log(object); // or anything else as per your wish
    }
    

提交回复
热议问题