I'm working on a Restful API with node.js and mongoose. I need to return an object with the results of a search. Here is my code:
var Get = function (criteria) {
var result;
Users.findOne(criteria, function(err, res){
console.log('Error: ' + err);
console.log('Results: ' + res);
result = err || !res ? err || errors['404']: res;
console.log(result);
});
console.log('Final results: ' + JSON.stringify(result));
return result;
};
Since I was having problems, I added those console.logs to watch where the result is lost. Here are the logs:
Final results: undefined
Error: null
Results: { //user... }
I need that result to be returned. How can I solve this?