I am new in node js and mongoose. I am trying to retrieve user from executed mongoose query on answer on: stack-overflow-answer
Here is my controller code (it contains both: callback and promises approach)
exports.getUser = (req, res, next) => {
var searchQuery = {...};
var result = [];
User.find(searchQuery, function (err, found) {
console.log(" >>>> inside", found)
result = found;
}
)
console.log(" >>>> outside ", result)
var result2 = [];
User.find(searchQuery)
.exec()
.then(function(found){
console.log(" >>>> inside", found)
result2 = found
return found
}
)
console.log(" >>>> outside ", result2)
res.status(200).json(result)
}
In both cases in console it is printing query result inside callback and outside it remains empty, why?
Thanks for help :)