0
私はこのコードをJavascriptでSailsとMongoDBを使用していますが、コードの実行は私が期待しているとおりではありません。コードの最初の部分は最初に実行されますが、 "foundwaiters"配列の追加は、HTMLページが表示されたときに配列が更新されないため、問題のある最後の部分(ログアウトした後のケース)でのみ実行されます。 これを修正するにはどうすればよいですか?ありがとうございました。javascriptの非同期実行をwaterlineを使用して
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-コール) –