我使用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