I am currently working on utilizing the 'csv-parser' module within a node.js environment. After successfully reading my csv document, I have managed to obtain an array named 'results' containing all the necessary information in .json format. Strangely, the first console.log displays all the data as expected, but the second one prints out an empty array (as initially declared). Can anyone shed light on why this scope issue is occurring and provide guidance on how to resolve it? Thank you in advance for your assistance.
const csv = require('csv-parser');
const fs = require('fs');
let results = [];
fs.createReadStream('MyData.csv')
.pipe(csv())
.on('data', data => results.push(data))
.on('end', () => {
console.log(results) //1st console.log
console.log('CSV file successfully processed');
});
console.log(results);//2nd console.log