我想收集从尝试保存数据到集合的所有错误(如果有的话),但它永远不会在回调中完成。有人能帮帮忙吗?
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);
}));发布于 2017-10-17 16:38:00
如果既没有调用debugLog('Saving data to history collection');也没有调用debugLog('Data already exists');,那么HistoryModel.find可能已经失败了。
我会将.catch移到promise链的末尾,并对errorResponse返回的错误执行throw,最后在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);
}));https://stackoverflow.com/questions/46785773
复制相似问题