I am currently working on a project (Node.js with Express) in which I have to import a CSV file, given a specific URL and then populate my Mongo database with it's data.
I'm pretty new to Node, so I might be doing something awfully wrong, but in my researches I found this way to try and get the data from a remote CSV file:
methods.getStops = function (req, res) {
var line = req.params.line;
var url = "/apiTransporte/Apresentacao/csv/gtfs/onibus/paradas/" +
"gtfs_linha{line}-paradas.csv".format({line: line});
var parsed = [];
var http = require('http');
var options = {
host: 'dadosabertos.rio.rj.gov.br',
port: 80,
path: url
}
http.get(options, function(response) {
response.on('data', function( data ) {
csv_data = data.toString();
csv_data = csv_data.replace("", "\r");
jsonified = [];
csv(csv_data, {columns: true, rowDelimiter: 'unicode'}, function (err, output) {
console.log("parsing data");
if (err)
console.log("ERROR: ", err);
jsonified.push(output);
});
console.log("data parsed", jsonified);
} );
console.log("Got response: " + response.statusCode);
}).on('error', function(e) {
console.log(url);
console.log("Got error: " + e.message);
});
}
This is my view that should take care of getting the data from a remote CSV file. However, I am having issues when trying to get the response, apparently it comes in chunks. I tried concatenating it into a string outside the response.on('data') callback with something like:
var response_data = "";
response.on('data', function (data) {
response_data += data;
...
but I am having trouble accessing the response_data after it should be populated. If I log it with console.log instead adding it to a string, the expected CSV shows on my terminal, broken in some points, which I believe be due to the fact that the callback is called whenever a chunk is retrieved.
My question is, how do I do to call a function after all the CSV is read, and my string "response_data" has all the data from the CSV file? So I can correctly parse it into a JSON object and store into my MongoDB?
Thank you for your help
Aucun commentaire:
Enregistrer un commentaire