I'm build a set of object to be saved in mongoDB from a csv stream. For each csv row I need to verify before saving that the object or objects do not exists in MongoDB. The below code runs inside a GET route.
I have been trying to use async waterfall but it does not behave the way I expected.
Here is the code
async.waterfall([
function (callback) {
console.log('in function 1');
--> Diagnosis.findOne({name: diagnosisName}, function (doc){
console.log(JSON.stringify(doc))
})
callback(null);
},
function (callback) {
console.log('in function2')
callback(null)
}],
function(err, results) {
console.log('finished!')
res.send("complete");
})
I would expect this to return the following
in function1
doc object in JSON
in function2
finished!
instead I get
in function 1
in function2
finished!
null
it runs as expected for as long as there is no findOne() call. What am I missing??
Much appreciated
findOne
is an async function. You would need to move the callback inside this function to follow the order you expect.But why not use the internal promise (or another promise lib)?