1

I am using the 'csv-parser' library. Using the method below I am able to get the data stored in the birthdays array shown in the console:

 const birthdays = []; 
fs.createReadStream('./data/groupes.csv')
 .pipe(csv({}))
 .on('data', (data) => birthdays.push(data))
 .on('end', () => {
 console.log(birthdays)
 });

but when I log outside the .on('end') like below, the console shows the birthdays array as empty:

 const birthdays = []; 
fs.createReadStream('./data/groupes.csv')
 .pipe(csv({}))
 .on('data', (data) => birthdays.push(data))
 .on('end', () => {
 });
 console.log(birthdays)

Why is that? and how can I solve it? Thanks in advance.

asked May 1, 2022 at 21:02

2 Answers 2

2

Why is that?

It's because createReadStream is working asynchronously and when you console.log(birthdays); outside, the code execution reads the log before createReadStream has finished processing

how can I solve it?

You were correct to put it inside the .on("end")

const birthdays = []; 
fs.createReadStream('./data/groupes.csv')
 .pipe(csv({}))
 .on('data', (data) => birthdays.push(data))
 .on('end', () => {
 console.log(birthdays);
 // further processing with birthdays
 });
answered May 1, 2022 at 21:10
Sign up to request clarification or add additional context in comments.

3 Comments

As a follow up question, I am trying to assign a value of a variable already declared inside the .on('end') part but it doesn't work for some reason, any idea why?
@Mehdi I would be able to help if you could share how you are declaring the variable and what exactly isn't working (eg, if there is an error)
I have posted another question stackoverflow.com/questions/72099168/csv-parser-onend-nodejs Thank you.
0

Could you do something like this?

const csv = require("csv-parser");
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
const readFile = promisify(fs.readFile);
const birthdays = [];
const filePath = path.join(__dirname, "data", "groupes.csv");
readFile(filePath, "utf8").then((data) => {
 csv(data, {
 separator: ",",
 })
 .on("data", (row) => {
 birthdays.push(row);
 })
 .on("end", () => {
 console.log(birthdays);
 });
});
answered May 1, 2022 at 21:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.