我试图逐行读取csv,在mongodb中查找一个值,并在将读取流管道回写流之前对其进行转换。我正在使用快速csv库进行csv解析。
当调用下面的代码段时,它会继续到转换中的行console.log,然后挂起。即使我让进程超时,我也不会得到任何错误日志记录。
我缺少什么异步流结构的原理?
或者如何从这个示例中获得一些错误信息?尝试/抓住永远不会触发。
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();});
发布于 2016-09-20 23:02:12
Transform期望next()函数继续到下一行,但是您的代码在下一个函数上被卡住了。
试着纠正一下,
if (!err && docs.length > 0) {
***perform your operation here***
next();
} else {
next();
}
PS -抱歉这么晚才回复
https://stackoverflow.com/questions/34814165
复制相似问题