如何从findOne()mongoose返回结果

时间:2017-12-29 06:32:11

标签: javascript node.js mongodb express mongoose

我在Mongoose中使用findOne()编写了一个函数,我想稍后将返回的结果用于另一个函数。我怎么能这样做?谢谢!

 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 
    })

}

这是我稍后调用的函数

var newDeal = Deal.findDeal(dealRequest);

4 个答案:

答案 0 :(得分:1)

异步过程控制的典型问题,你应该使用promise或async / await,当然回调还可以,但不建议。

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;  
});
承诺风格:

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)
})

但我建议您使用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)
    }
})()

请注意,await必须在async函数中使用并基于promise。

答案 1 :(得分:0)

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});
})
}

在函数中添加res并使用res.send

答案 2 :(得分:0)

您可以改用Promise。

那么你的功能就是这样。

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;
})

其他文件中的某处

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

})

答案 3 :(得分:0)

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;
        });
    }
};

首先,将数据从内部函数返回到外部函数,然后从外部函数返回。