Suppose I have a collection in my mongoDB: db.co
and only have one document:
{ \"_id\" : ObjectId(\"50d083e32cdcf7ce065b616c\"),
\"age\" : 22
.find() works asynchronous, meaning that its callback runs when your function already has.
see variable scope in asynchronous function
//try passing a callback function to your search function, like:
function findSomeoneInCo (name, callback) {
//happens now - here you could still return something
Co.find({"name": name}, function (err, doc) {
//happens later - too late to return stuff, function has ran already
if (err) {
callback(err);
return;
}
callback(doc);
})
}
//call your function passing a callback function
findSomeoneInCo("lee", function(doc){
console.log('do something with your results: ' + doc);
});