简体   繁体   中英

How to return result from findOne() mongoose

I wrote a function using findOne() in Mongoose and I want to later use the returned result for another function. How could I do so? Thanks!

 module.exports.findDeal = function(dealRequest){
    Deal.findOne({name:dealRequest},function(err,newDeal){
        if (err) throw err;
        // twiml.message(newDeal.deal)
        console.log("returned from the model: ",newDeal)
        return 
    })

}

This is the function that I later call

var newDeal = Deal.findDeal(dealRequest);

typical question of asynchronous process control,you should use promise or async/await , of course callback is ok but don't suggest .

module.exports.findDeal = function(dealRequest, callback){ //callback function
    Deal.findOne({name:dealRequest},function(err,newDeal){
        if (err) callback(err)
        console.log("returned from the model: ",newDeal)
        callback(null, newDeal)
    })
}

Deal.findDeal(dealRequest, (err, result) => {  //callback function
    var newDeal = result;  
});

promise style:

module.exports.findDeal = function(dealRequest){
    return new Promise((resolve, reject) => {

        // still callback function is ok
        Deal.findOne({name:dealRequest},function(err,newDeal){
            if (err) reject(err)
            resolve(newDeal)
        })

        // mongoose return promise, both is ok
        Deal.findOne({name:dealRequest}).then(newDeal => {
            resolve(newDeal)
        }).catch(err => {
            reject(err)
        })
    })

}

Deal.findDeal(dealRequest).then(result => {
    var newDeal = result;
}).catch(err => {
    console.log(err)
})

but I suggest you use async/await:

module.exports.findDeal = async function (dealRequest) {  
    return new Promise((resolve, reject) => {
        try {
            const newDeal = await Deal.findOne({name:dealRequest});
            resolve(newDeal)
        } catch (error) {
            reject(error)
        }
    })
}

(async () => {
    try {
        var newDeal = await Deal.findDeal(dealRequest)
    } catch (error) {
        console.log(error)
    }
})()

note that await must use in async function and base on promise.

module.exports.findDeal = function(res,dealRequest){
Deal.findOne({name:dealRequest},function(err,newDeal){
    if (err) throw err;
    // twiml.message(newDeal.deal)
    console.log("returned from the model: ",newDeal)
    res.send({'result':newDeal});
})
}

add res in the function and use res.send

You can use Promise instead.

then your function would be like this.

module.exports.findDeal = function(dealRequest){
   return Deal.findOne({name:dealRequest},function(err,newDeal){
      if (err) throw err;
      // twiml.message(newDeal.deal)
        console.log("returned from the model: ",newDeal)
      return newDeal;
})

Somewhere in other file

const { findDeal } = require('thisfilename.js');
findDeal(somdealvalue).then(function(deal) {
  console.log(deal);

})
module.exports = {
    findDeal: function(dealRequest){
    return Deal.findOne({name:dealRequest},function(err,newDeal){
        if (err) throw err;
        // twiml.message(newDeal.deal)
        console.log("returned from the model: ",newDeal)
        return newDeal;
        });
    }
};

first, return data from inner function to outer function and then return from outer function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM