[英]using Async.each refuses to call the final callback
in the following code I am expecting the function finalCallBack to be executed once we are done iterating through all elements 在下面的代码中,我期望一旦完成所有元素的迭代,函数finalCallBack将被执行
var rows = [
{ name: 'first'},
{ name: 'second'}
];
var execForEachRow = function(row, callback){
var studentModel = new StudentModel(row);
studentModel.save(function(err,result){
if (err) { throw err;}
rowsSavedCount++;
});
}
var finalCallBack = function(err){
if (err) { msg = err;} else { msg = rowsSavedCount;}
res.send({"result" : msg});
}
async.each(rows, execForEachRow, finalCallBack);
When i execute the above code, it very successfully inserts data into the mongo collection. 当我执行上述代码时,它非常成功地将数据插入到mongo集合中。 However the finalCallBack does not get called. 但是,不会调用finalCallBack。
Any clue what I might be missing here ? 有什么线索我可能会在这里丢失吗?
You've missed calling callback in studentModel.save
's callback: 您已经错过了在studentModel.save
的回调中调用回调:
studentModel.save(function(err,result){
if (err)
return callback(err);
rowsSavedCount++;
callback(null);
});
Also - throwing Exception is not a good idea - it'll break you whole Express server. 另外-抛出Exception并不是一个好主意-它会破坏整个Express服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.