Search code examples
javascriptnode.jscsvnode-csv-parse

Unable to push data into an array when reading csv using createReadStream


I am trying to store data from a csv file in an array using createReadStream in node.js but for some reason the array stays empty and no data is stored in it. When i console log the values where i'am trying to push the data it logs it just fine. I've tried both csv-parser and csv-parse for this but none of them seem to work.

const fs = require("fs");
const csv = require('csv-parser');

function parseCSV () {
    let finaldata = []
    fs.createReadStream('data.csv')
    .pipe(csv())
    .on('data', function (row){
        finaldata.push({Userid: row[0], Timestamp: row[1]})
    }).on('end', function(){
        console.log(finaldata)
    })
}
parseCSV()

Solution

  • I fixed this using the following code below. I kept experimenting to land on something that finally worked. I have no idea why this fix works but i would love an explanation.

    The change i made here is to call the Object constructor instead of using {} so finaldata.push(Object(row)) instead of finaldata.push({Userid: row[0], Timestamp: row[1]})

    const fs = require("fs");
    const csv = require('csv-parser');
    
    const parseCSV = () => {
        let finaldata = []
        fs.createReadStream('data.csv')
        .pipe(csv())
        .on('data', (row) => {
            finaldata.push(Object(row))
        }).on('end', () => {
            console.log(finaldata)
        })
    }
    parseCSV()