2017-06-21 12 views
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 
       }); 
      } 
+1

[非同期呼び出しからの応答を返すにはどうすればよいですか?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-コール) –

答えて

0

あなたのコードを約束することができます。オフトピックですが、これがセイルの場合は、ES2015以降のアンダースコアを使用することができます。 find()をコードのダンプに書き直すことができるかもしれません。

関連する問題