0
我在Javascript中使用Sails和MongoDB來創建代碼,但代碼的執行並不像我期待的那樣。代碼的第一部分首先被執行,但「foundwaiters」數組的添加僅在最後(在註銷情況之後)執行,這是一個問題,因爲當HTML頁面顯示時數組未更新。 我該如何解決這個問題?謝謝。使用水線在javascript中執行異步執行
Tutorial.find(...)
.populate('videos')
.exec(function (err, foundTutorials){
_.each(foundTutorials, function(tutorial){
_.each(tutorial.videos, function(video){
User.find().populate('isWaiting').exec(function (err, foundusers){
_.each(foundusers, function(user){
_.each(user.isWaiting, function(myvideo){
if (myvideo.id === video.id) {
foundwaiters.push(user);
}
});
});
});
});
});
// The logged out case
if (!req.session.userId) {
return res.view('profile', {
// This is for the navigation bar
me: null,
// This is for profile body
username: foundUser.username,
gravatarURL: foundUser.gravatarURL,
frontEnd: {
numOfTutorials: foundTutorials.length,
numOfFollowers: foundUser.followers.length,
numOfFollowing: foundUser.following.length,
/// ###########
numOfWaiters:foundwaiters.length
// ############
},
// This is for the list of tutorials
tutorials: foundTutorials,
// #######
waiters:foundwaiters
});
}
可能的重複[如何從異步調用返回響應?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-呼叫) –