我写了一个小的
async
脚本,批量插入很多JSON文件到MongoDB分片集群.这是我第一次使用这个模块(我还在学习Node.js).我不知道我做得对.
>代码是瀑布的最后一部分(1):以前的函数结束
使用db,coll和files属性的对象.
> files数组包含数百个文件路径和函数
适用于数组的每个元素,同样是瀑布(2).
>瀑布(2)由以下内容组成:读取,解析,插入.当这个瀑布结束时(3)我调用complete来完成数组中单个项的处理,传递错误(如果有的话).
到目前为止这么好,对吗?
我无法理解的是async.eachLimit回调(4)中发生的事情.从文档:
A callback which is called after all the iterator functions have
finished, or an error has occurred.
也就是说,当所有函数都完成后,next()调用(5)结束脚本.但是,根据文档,在发生单个错误时会调用相同的回调函数(4).这是我的脚本在发生单个文件失败时停止.
我怎么能避免这个?
async.waterfall([ // 1
// ...
function (obj, next) {
async.eachLimit(obj.files, 1000,
function (file, complete) {
async.waterfall([ // 2
function (next) {
fs.readFile(file, {}, function (err, data) {
next(err, data);
});
},
function (data, next) { // Parse (assuming all well formed)
next(null, JSON.parse(data));
},
function (doc, next) { // Insert
obj.coll.insert(doc, {w: 1}, function (err, doc) {
next(err);
});
}
], function (err, result) { // 3
complete(err);
});
},
function (err) { // 4
if (err) console.error(err);
next(null, obj); // 5
}
);
}
], function (err, obj) { // Waterfall end
if (err) console.error(err);
obj.db.close(); // Always close the connection
});
最佳答案 如果你不希望它在出现错误时中断,你应该用一个假的第一个参数来调用回调,就像这样(看看// 3).
这对你好吗/我理解正确吗?
async.waterfall([ // 1
// ...
function (obj, next) {
async.eachLimit(obj.files, 1000,
function (file, complete) {
async.waterfall([ // 2
function (next) {
fs.readFile(file, {}, function (err, data) {
next(err, data);
});
},
function (data, next) { // Parse (assuming all well formed)
next(null, JSON.parse(data));
},
function (doc, next) { // Insert
obj.coll.insert(doc, {w: 1}, function (err, doc) {
next(err);
});
}
], function (err, result) { // 3
if (err) {
console.log(file + ' threw an error');
console.log(err);
console.log('proceeding with execution');
}
complete();
});
},
function (err) { // 4
next(null, obj); // 5
}
);
}
], function (err, obj) { // Waterfall end
if (err) console.error(err);
obj.db.close(); // Always close the connection
});