I want to parse a csv file into an array so I can use it to map data later.
I tried multiple libraries like csv-parser, fast-csv but for some reason the data doesn't get stored in the array (I believe it has to do with the fact that the method createReadStream is async)
The code below prints the data to the console but when I log it I get an undefined.
const fs = require('fs');
const csv = require('csv-parser');
let csvPath = './file.csv';
const results = [];
fs.createReadStream(csvPath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});
//prints 0
console.log(results.length);