【问题标题】:Fast-CSV stream transform async快速 CSV 流转换异步
【发布时间】: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();});

【问题讨论】:

    标签: node.js mongodb csv


    【解决方案1】:

    Transform 期望 next() 函数继续执行下一行,但您的代码卡在 next 函数上。

    试试下面的修正,

    if (!err && docs.length > 0) {
                    ***perform your operation here***
                    next();
                } else {
                    next();
                }
    

    PS - 抱歉这么晚才回复

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多