javascriptnode.jsstreamoutputnode-csv-parse

How to obtain the output after parsing the csv using csv-parser?


I am parsing a huge csv (1.2GB) using csvparser and trying to obtain certain column data from the parsed csv. I am trying to push the data to the array after processing, but only getting empty array as output. How can I fix this code?

var parse = require('csv-parse');

var output = []
var parser = parse({
   delimiter: '\t',
   columns: true
}, function(err, csvLine) {
   for (var l = 0; l < csvLine.length; l++) {
      output.push(csvLine[l].id)
   }
});

console.log(output)

fs.createReadStream('file.csv', {
   encoding: 'utf8'
}).pipe(parser);

The output at console.log(output) is always an empty array. Please help me solve this.

I tried to understand the post here - Save csv-parse output to a variable. But I was not able to understand and fix the code.


Solution

  • Because of asynchronous. The line console.log(output) runs after the declaration of parser variable. At that time output variable has no values in it.

    You have to access output variable after the loop when all data read from csv as given below.

        var parse = require('csv-parse');
        var fs = require('fs');
        var output = []
        var parser = parse({
           delimiter: ',',
           columns: true
        }, function(err, csvLine) {
           for (var l = 0; l < csvLine.length; l++) {
              output.push(csvLine[l].visitortype)
           }
           console.log(output)
        }); 
        fs.createReadStream('file.csv', {
           encoding: 'utf8'
        }).pipe(parser);