【发布时间】:2013-05-02 16:29:47
【问题描述】:
我写了一个小async 脚本,将大量 JSON 文件批量插入到 MongoDB 分片集群中。这是我第一次使用这个模块(我还在学习 Node.js)。我不知道我做得对不对。
- 代码是瀑布的最后一部分(1):前面的函数结束
使用具有
db、coll和files属性的对象。 -
files数组包含数百个文件路径和函数 apply 到数组的每个元素同样是一个瀑布 (2)。 - 瀑布 (2) 由以下部分组成:读取、解析、插入。当这个瀑布结束时 (3) 我调用
complete来完成对数组中单个项目的处理,并传递错误(如果有的话)。
到目前为止一切顺利,对吗?
我无法理解的是 async.eachLimit 回调 (4) 中发生了什么。来自文档:
在所有迭代器函数完成后调用的回调 完成,或发生错误。
也就是说,当所有函数都完成后,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
});
【问题讨论】:
标签: node.js mongodb asynchronous callback asynccallback