【发布时间】:2016-09-21 17:31:35
【问题描述】:
我正在尝试逐行读取 csv,在 mongodb 中查找值并在将读取流传输回写入流之前对其进行转换。我正在使用 fast-csv 库进行 csv 解析。
当调用下面的 sn-p 时,它会前进到转换内的 console.log 行,然后挂起。即使我让进程超时,我也没有收到任何错误记录。
我缺少什么异步流结构原理?
或者我如何从这个例子中得到一些错误信息? Try/catch 永远不会触发。
var db = mongojs("mongodb://127.0.0.1:27017/", ['collection']);
fs.createReadStream(path)
.pipe(csv.parse({headers: false}))
//pipe the parsed input into a csv formatter
.pipe(csv.format({headers: true}))
//Using the transform function from the formatting stream
.transform(function(row, next){
console.log(row);
db.collection.find({email: row[0].toString()}, function (err, docs) {
console.log(docs[0]);
console.log(err);
if (!err && docs.length > 0) {
next(void 0, {
email: docs[0].email,
name: docs[0].name
});
} else {
next(err)
}
});
})
.pipe(fs.createWriteStream("tmp/list.csv"))
.on("finish", function() {upload();});
【问题讨论】: