2017-10-17 69 views
0

我想收集所有的錯誤(如果有的話),試圖將數據保存到集合,但它永遠不會完成回調。有人可以幫忙嗎?Async.reflect with Async.mapLimit

async.mapLimit(results, 5, async.reflect((result, callback) => { 
    debugLog('Checking for repeating data'); 
    return HistoryModel.find({ 
     gameId: result.gameId, 
     endDate: result.endDate 
    }) 
    .then(response => { 
     if (!response || response.length === 0) { 
     debugLog('Saving data to history collection'); 
     return HistoryModel.create(result) 
      .then(() => callback()) 
      .catch(err => callback(err, null)); 
     } else { 
     debugLog('Data already exists'); 
     return callback(
      errorResponse('result', 
      `The data with ${result.gameId} and ${result.endDate} already exists`), null); 
     } 
    }) 
}, (err, results) => { 
    console.log(err); 
    console.log(results); 

    res.status(200).send(results); 
})); 

回答

0

如果沒有debugLog('Saving data to history collection');也不debugLog('Data already exists');被調用,那麼HistoryModel.find可能已經失敗。

我將在.catch移動到你的諾言鏈的末端,並通過throw返回errorResponse錯誤,並做在catch整個錯誤處理結尾。

async.mapLimit(results, 5, async.reflect((result, callback) => { 
    debugLog('Checking for repeating data'); 
    return HistoryModel.find({ 
    gameId: result.gameId, 
    endDate: result.endDate 
    }) 
    .then(response => { 
    if (!response || response.length === 0) { 
     debugLog('Saving data to history collection'); 
     return HistoryModel.create(result) 
      .then(() => callback()) 
    } else { 
     debugLog('Data already exists'); 
     throw errorResponse('result', 
          `The data with ${result.gameId} and ${result.endDate} already exists`) 
    } 
    }) 
    .catch(err => { // catch all error that happen in the Promise chain 
    callback(err, null) 
    }) 
}, (err, results) => { 
    console.log(err); 
    console.log(results); 

    res.status(200).send(results); 
}));