I am using node.js and mongoose to carry out several HTTP requests including a get request. My get function will have quite a bit of functionality and to ease handling of many of the data, I am trying to have local variables store the returns from mongo. For example:
router.get('/getstuff/:test', function(req, res) {
var testId = req.params.test;
var returnStuff = null;
var collection = req.collection;
collection.find({userIdd : testId}, function(err, data){
if (err) console.log(err);
else {
console.log(data); // works, data is shown in log
returnStuff = data; // does not work, data is not saved to returnStuff
}
});
console.log(returnStuff); // undefined
res.send();
});
I am trying to get what I get back from the database, an array, to the returnStuff variable, but due to the closure, I am unable to do so. This may seem trivial, but as I said, I will have a lot more operations and this would really simplify things.
Anyone have any tips? Really appreciate it.
Thanks