2016-03-29 29 views
0

問題が発生しました。基本的に、この非同期の滝が完了したら、関数を呼び出す必要があります。問題は私がアップデートを呼び出すことですが、アップデートが完了するまで続きます。呼び出したい機能は、すでに更新されているこのデータに依存します。タスクが完了する前にNodeJS非同期処理が終了する

メッセージは送信されていません。しかし、最終的にデータベースで更新されます。

function(arg1, arg2, callback) { 
    console.log('Updating current_pot with amountInPot'); 
    current_pot.findOneAndUpdate(
     {}, 
     {'amountInPot': currentlyInPot}, 
     function(err, current) { 
      if (err) { 
       throw err; 
      } 
      if (current != null) { 
       console.log('Updated current_pot amountInPot'); 
       callback(null, 'c'); 
       //This message should be sent before the callback continues! 
      } 
     }); 
    } 

すべてのコード:最後の行で

async.waterfall([ 
    function(callback) { 
     //remove from db 
     console.log('Add to local pot!'); 
     currentlyInPot++; 
     peopleInPot.push([user.local.picture, message[1], user.local.email]); 
     callback(null, 'a', 'b'); 
    }, 
    function(arg1, arg2, callback) { 
     console.log('Updating persons credits'); 
     callback(null, 'c', 'd'); 
    }, 
    function(arg1, arg2, callback) { 
     console.log('Adding new people to db'); 
     var newPeople = new people_pot(); 
     newPeople.email = user.local.email; //Auto increment 
     newPeople.picture = user.local.picture; 
     newPeople.message = message[1]; 

     /** success starts */ 
     newPeople.save(function(err) { 
      if (err) { 
       throw err; 
      } 
     }); 

     callback(null, 'a', 'b'); 
    }, 
    function(arg1, arg2, callback) { 
     console.log('Updating current_pot with amountInPot'); 
     current_pot.findOneAndUpdate({}, { 
      'amountInPot': currentlyInPot 
     }, 
    function(err, current) { 
     if (err) { 
      throw err; 
     } 
     if (current != null) { 
      console.log('Updated current_pot amountInPot'); 
      callback(null, 'c'); 
      //This isn't getting completed before the loadpot is called! 
     } 
    }); 
} 
], function(err, result) { 
    // result is 'e' 
    //add to db 
}); 

答えて

1

newPeople.saveが完了するのを忘れました。コールバック関数のコールをコールバックに入れます。

/** success starts */ 
    newPeople.save(function(err) { 
     callback(err, 'a', 'b'); 
    }); 
0

使用.then()し、そこに機能を追加します。

+0

これは質問に対する回答ではありません。十分な[評判](http://stackoverflow.com/help/whats-reputation)があれば、[任意の投稿にコメントする]ことができます(http://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの説明を必要としない回答を提供する](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューの投稿](レビュー/低品質の投稿/ 11812032) –

+0

これは答えではないことをどのように知っていますか? – user3151766

+0

答えを説明しようと常にしてください。一行の答えは、あなたをうまくすることはありません。答えをOPのコードと組み合わせてみてください。あなたの答えはコメントのようです。 –

関連する問題