gpt4 book ai didi

javascript - 无法使用异步 waterfall 运行 parse-csv 函数

转载 作者:行者123 更新时间:2023-11-30 17:22:31 24 4
gpt4 key购买 nike

我正在尝试请求一个 url,从中获取一个 csv 文件,然后将该 csv 转换为 json。我正在使用 request , asynccsv-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,无论我是什么 console.log

最佳答案

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

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: 'Unsanitary Conditions', 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: 'Vegetation', 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

关于javascript - 无法使用异步 waterfall 运行 parse-csv 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com