Reputation: 83
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);
Upvotes: 4
Views: 7029
Reputation: 545
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 307
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);
})
Upvotes: 2
Reputation: 1
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
Upvotes: 0