无法使用异步瀑布运行parse-csv函数

时间:2014-07-21 23:34:59

标签: javascript node.js csv asynchronous

我正在尝试请求一个url,从中获取一个csv文件,然后将该csv转换为json。我正在使用requestasynccsv-parser

我有这段代码:

var fs = require('fs'),
    async = require('async'),
    request = require('request'),
    csv = require('csv-parser'),
    fileUrl = 'http://forever.codeforamerica.org/fellowship-2015-tech-interview/Violations-2012.csv'

var getData = function(cb){
    request(fileUrl, function(err, response, body){
        (err) ? cb(err) : cb(null, body);
    })
};

var parseCsv = function(csvData, cb){
    var violations = [];
    fs.createReadStream(csvData)
        .pipe(csv())
        .on('data', function(violation){
            violations.push(violation)
        })
        .on('end', function(){
            cb(null, violations)
        })
}

// run the functions
async.waterfall([
    getData,
    parseCsv
], function(err, results){
    if (err) return err;

    console.log('this does not log')
})

我得到的文件很好,但是每次它都会注销csv数据,而不是json,无论我是什么控制台.log

1 个答案:

答案 0 :(得分:2)

管道处理不正确,您可以将“请求”响应直接传递到管道中。

var fs = require('fs'),
    async = require('async'),
    request = require('request'),
    csv = require('csv-parser'),
    fileUrl = 'http://forever.codeforamerica.org/fellowship-2015-tech-interview/Violations-2012.csv';

var parseCsv = function(cb){
    var violations = [];
    request.get(fileUrl)
    .pipe(csv())
        .on('data', function(violation){
            violations.push(violation);
        })
        .on('end', function(){
            cb(null, violations);
        });
};

// run the functions
async.waterfall([
    parseCsv
], function(err, results){
    console.log(err, results);
});

输出:

  

{violation_id:'225880',       inspection_id:'289908',       violation_category:'不卫生条件',       violation_date:'2012-10-17 00:00:00',       violation_date_closed:'2012-11-08 00:00:00',       violation_type:'Unsanitary Floors or Walls'},{violation_id:'225905',       inspection_id:'289962',       violation_category:'植被',       violation_date:'2012-10-26 00:00:00',       violation_date_closed:'2013-01-07 00:00:00',       violation_type:'Overgrown Vegetation'},{violation_id:'224854',       inspection_id:'288325'

PS:类似问题here