我正在使用Async實用程序模塊從Mongodb數據庫返回項目。我想要異步執行。嘗試返回這些項目時遇到問題。在返回所有數據之前儘早完成循環中的異步mongodb查找
一旦所有的User.find()'s
已完成,我想發起回調,現在async.each()
提前終止,並且只有當它應該全部返回時給我一個來自數據庫的項目。
的代碼如下:
async.each(lessons, function(lesson, next) { // For each item in lesson array
if (_.isEmpty(lesson.lesson_grades) == true) { // Check if the grades array is empty
return;
} else {
async.each(lesson.lesson_grades, function(grade, next) { // For each grade in grade array
User.find({ // Find user from grade user_id
_id: grade.user_id,
}, '-salt -hashedPassword', function(err, user) {
grade["name"] = user[0].name; // Add name
grade["email"] = user[0].email; // Add email
next(); // !! I think this is where the problem lies, it fires next() once the first item has been returned - so it doesn't get to the other items !!
});
}, function(err) {
next(lessons);
});
}
}, function(lessons, err) {
return res.json(200, lessons); // Return modified lessons (with name and email) to browser, currently only returns one but returns them all if a setTimeout() is added, making it a premature callback problem
});
有人能指出我如何正確地做這個正確的方向?我應該跟蹤迭代嗎?任何幫助,將不勝感激。
感謝您花時間回答。我已經做出了您建議的更改,但我仍然遇到同樣的問題。你能指導我如何改寫它嗎?自你發佈這個答案以來,我一整天都在嘗試。謝謝。 – 2014-11-09 06:21:44
你是如何得到這個輸出的?另外,請注意我對我的回答的改變 - 最後的函數不應該有'lessons'作爲參數(它應該是一個閉包變量)。 – 2014-11-09 16:05:43