2016-02-12 42 views
3

過去幾天我一直試圖完成以下操作,而我無法解決它。我覺得我已經嘗試了一切。所以這裏...緩存來自異步函數的結果,並將其傳遞給Async.js中的下一個函數

我的路線被賦予一個JSON對象,其中包含所有信息來創建一個新的測驗。 換句話說 - 一個包含有關新測驗信息的對象,一系列問題,其中每個問題都包含一組答案。

我使用async.js瀑布功能,並要做到以下幾點:

  • 保存新的測驗,DB,並通過新的測驗ID從數據庫返回到下一個瀑布功能

  • 遍歷每個問題。緩存從數據庫返回的新問題ID,並將它們傳遞給下一個瀑布函數。這裏的問題是緩存ID。由於該功能是異步的,我不能緩存結果的任何地方,以在未來的函數中使用...

  • 循環每一個答案,並將其保存到DB

這是我得趕緊:

router.post('/quiz/create', function (req, res) { 
    // JSON object with the new Quiz 
    var json = req.body; 
    async.waterfall([ 
     function(callback) { 
      // Returns a Quiz ID for the new quiz, and passes it to the next waterfall function 
      db.createQuiz(json, function(err, result) { 
       // ID returned from DB is formatted as [{'': id}], hence result[0][''] 
       callback(null, result[0]['']); 
      }); 
     }, 
     function(quizID, callback) { 
      // Loop through each question in the quiz 
      async.forEachSeries(json.questions, function(question, callback) { 
       // Save the question to DB, and get the new question ID returned 
       db.createQuestion(question, quizID, function(err, result) { 
        // TODO: cache the new question ID's in an array somewhere to be passed to the next waterfall function 
        // Start next iteration of the loop 
        callback(); 
       }); 
      }, function(err) { 
       // Done with all questions. Pass question ID's to next function 
       callback(null, cachedQuestionIDs); 
      }); 
     }, 
     function(cachedQuestionIDs, callback) { 
      // TODO: access the question ID's, and use them to loop through and save answers to DB 
     } 
    ], function(err, result) { 
     res.json({ 
      success: true, 
      message: 'Quiz created!' 
     }); 
    }); 
}); 

回答

2

的值只是存儲在async.forEachSeries()以外但在async.waterfall()控制流程的包裹第二函數內的陣列。你可以在你的async.forEachSeries()執行完畢後返回這個數組。

function(quizID, callback) { 
    var cachedQuestionIDs = []; 

    // Loop through each question in the quiz 
    async.forEachSeries(json.questions, function(question, callback) { 
    // Save the question to DB, and get the new question ID returned 
    db.createQuestion(question, quizID, function(err, result) { 
     // Store our result 
     cachedQuestionIDs.push(result); 

     // Start next iteration of the loop 
     return callback(); 
    }); 
    }, function(err) { 
    // Done with all questions. Pass question ID's to next function 
    return callback(null, cachedQuestionIDs); 
    }); 
} 
+0

謝謝:)它的奇怪,因爲所有的時候,我曾試圖以另一種範圍內的ID添加到一個數組,這將永遠是'undefined'。 我現在可以在瀑布中的最後一個函數中訪問ID。 我會盡量讓其餘的人明天上班。 雖然我不禁覺得我的方法有點骯髒。 在第二個瀑布函數中嵌套循環會更正確。對於我添加到數據庫中的每個問題,我可以遍歷所有答案,並將它們循環到同一個函數中的數據庫中。 但我不知道如何做到這一點與所有的異步事情。 – Petter

1

我終於得到它正常工作,與一個更優雅的解決方案使用嵌套forEachSeries循環的問題和答案。這裏沒有錯誤處理。只是爲了展示基本的想法。這是瀑布中的第二個功能。

function(quizID, callback) { 

     async.forEachSeries(json.questions, function(question, callback) { 
      db.registerQuestion(question, quizID, function(err, result) { 
       async.forEachSeries(question.answers, function(answer, callback) { 
        db.registerAnswer(answer, result[0][''], callback); 
       }, function() { 
        callback(); 
       }); 
      }); 
     }, function() { 
      callback(); 
     }); 

} 
相關問題