I'm creating an application with SailsJS. I have a question about using callbacks within an Async waterfall and Async each.
I've got two functions inside an async waterfall function. The first callback returns the correct value. The second function has a nested callback. However, the value of the nested callback is stuck within the waterline find function, and never gets returned to the outer function.
Do you know how I could return the value to the outer function?
Thanks for your help.
myFunction: function (req,res) {
async.waterfall([
function(callback){
// native MongoDB search function that returns an Array of Objects
},
function(result, callback){
async.each(resultAll, function (location){
Model.find({location:'56d1653a34de610115d48aea'}).populate('location')
.exec(function(err, item, cb){
console.log (item) // returns the correct value, but the value cant be passed to the outer function
})
})
callback(null, result);
}], function (err, result) {
// result is 'd'
res.send (result)
}
)}
When you use async's function you should you should use you callback once you are done with operations you want to perform. for example:
so according to me your code should be like
Apply this and you can see things you want in your final callback.