I am trying to request a url, get a csv file from it and then turn that csv into json. I am using request, async and csv-parser
I have this code:
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')
})
I get the file fine, but each time it logs out the csv data, not the json, no matter what I console.log
Piping was not handled correctly, you can pass the 'request' response directly into a pipe.
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);
});
OUTPUT :
{ 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: Similar problem here