I'm reading a csv files to pass it to another functions and store the records inside a database. I'm using csv-parser library, it shows on console all the lines but not stores it on an array.
const fs = require("fs");
const csv = require('csv-parser')
export const readFile = (file: string): any[] => {
let lines: any[] = [];
fs.createReadStream(`./input/${file}`)
.pipe(csv({ separator: ';', headers: false }))
.on('data', (data) => lines.push(Object.values(data)))
.on('data', data => console.log(Object.values(data)))
.on('data', data => logger.debug(`dataAccess LN 51`))
.on('data', data => logger.debug(Object.values(data)))
.on('end', () => {
logger.debug(`dataAcces LN 54`);
logger.debug([lines]);
console.log(lines);
});
logger.debug(`dataAcces LN 64`);
logger.debug([lines]);
return [lines];
}
The lines value it's always empty array.