我使用node.js和猫鼬进行包括get请求在内的多个HTTP请求。我的get函数将具有相当多的功能,并且可以轻松处理许多数据,我尝试让局部变量存储来自mongo的返回值。例如:保存GET请求返回从node.js和猫鼬变量
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();
});
我试图得到什么我从数据库中,数组的returnStuff变回来,但由于关闭,我不能这样做。这可能看起来微不足道,但正如我所说,我会有更多的操作,这将真正简化事情。
任何人有任何提示吗?真的很感激它。
感谢
另请注意“[如何避免Node.js中异步函数的长嵌套](http://stackoverflow.com/questions/4234619/how-to-avoid-long-nesting-of-asynchronous-functions -in-node-js)“ – 2014-10-10 02:22:18