Here is the code I am running
const fs = require('fs');
const csvParse = require('csv-parse');
function getValue() {
let results = [];
fs.createReadStream('./assets/myCSV.csv')
.pipe(csvParse({delimiter: '\n'}))
.on('data', (data) => results.push(data))
.on('error', (err => {
console.log(`csv-parse error from getValue: ${err}`);
}))
.on('end', () => {
console.log('finished!');
let csvLength = Object.keys(results).length;
// getting a random value within the csv
min = Math.ceil(0);
max = Math.floor(csvLength);
let randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
let randomCSVLine = results[randomNumber];
console.log(randomCSVLine);
return randomCSVLine;
});
}
console.log(`mine: ${getValue()}`);
here are the results and I dont know why the value doesnt show. I think it could be an async issue but not sure how to solve it yet results:
mine: undefined
finished!
[ '10' ]