1

Problem

The push() function inside the createReadStream does not push my data to the array.

The Code

let csv = require('csv-parser');

let people = [];

let final_array = [];

fs.createReadStream('people.csv')
      .pipe(csv())
      .on('data', (data) => {
        people.push(data)
      })
      .on('end', () => {
        console.log(people) //This prints out people fine
      })

console.log(people) //This returns empty array

people.forEach((names) => {
    console.log(people[names]); //absolutely no output
})

The Output

[] //Output for console.log(people) outside the createReadStream

//Output for console.log  people inside the createReadStream
[
  { username: 'rezero' },
  { username: 'software' },
  { username: 'planets' },
  { username: 'darkseid' },
  { username: 'gintama' },
  { username: 'websines' },
  { username: 'digital' }
]

Desired Output

I want only the usernames from the data to go into my final_array for further tasks.

final_array = ['rezero','software','planets','darkseid','gintama','websines','digital']

1 Answer 1

1

The csv-parsing will run asynchronously, which means console.log(people) (and the code after that) will run before the file was parsed completely.

You could wrap the parsing into a promise and await this promise (this still needs error handling, but should give you an idea):

(async () => {
    let peopleData = [];
    await new Promise(resolve => {
        fs.createReadStream('people.csv')
            .pipe(csv())
            .on('data', (data) => {
                peopleData.push(data)
            })
            .on('end', () => {
                resolve();
            })
    });
    const finalArray = peopleData.map(data => data.username);
    finalArray.forEach(name => {
      console.log(name);
    });
    
})();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, can you also help me loop the objects as shown in Desired Output?
Sure - I've updated the answer with a simple .map(), please check.

Your Answer

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.