'Accessing Parsed CSV data , CSV-Parser Node.js

Im using csv-parser npm package and doing a sample csv parse. My only confusion is accessing the parsed array after running these functions. I understand im pushing the data in .on('data') , then doing a console.log(results); statement in .on('end'); to show what's being stored. Why do I get undefined when i try to access results after running those functions. Doesn't results get the information stored?

const csv = require('csv-parser');
const fs = require('fs');
const results = [];


fs.createReadStream('demo.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
    console.log(results);
});


Solution 1:[1]

I can get results in .on('end', () => { console.log(results);}); , but if I put a console.log() after the createReadStream , results is undefined, does that make sense? – Videoaddict101

Your stream acts asynchronously, that means your data and your end handler will be called later, meanwhile your javascript continue to be executed. So accessing your array just after fs.createReadStream instruction will result of an empty array.

Understanding async is very important using javascript, even more for nodejs.

Please have a look on differents resources for handling async like Promise, Async/Await ...

Solution 2:[2]

I came here to find the solution to the same issue. Since this is an async operation, what works here is to call that function that acts on your parsed data once the end handler is called. Something like this should work in this situation:

const csv = require('csv-parser');
const fs = require('fs');
const results = [];

fs.createReadStream('demo.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
    console.log(results);
    csvData(results);
});

const csvData = ((csvInfo) => {
    console.log(csvInfo);
    console.log(csvInfo.length);
})

Solution 3:[3]

You should you neat-csv which is the endorsed wrapper for csv-parser that gives you a promise interface.

That said, you can create a promise and resolve it in the on("end", callback)

import fs from "fs";
import csv from "csv-parser";

function getCsv(filename) {
  return new Promise((resolve, reject) => {
    const data = [];
    fs.createReadStream(filename)
      .pipe(csv())
      .on("error", (error) => reject(error))
      .on("data", (row) => data.push(row))
      .on("end", () => resolve(data));
  });
}

console.log(await getCsv("../assets/logfile0.csv"));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 viksidada
Solution 3