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-呼叫) –