Votemike
Votemike

Reputation: 882

npm csv-parse not reading all rows

I'm trying to read a CSV using csv-parse, but the final line isn't being read. My code:

const handleFiles = event => {
  const f = event.target.files[0];
  const parser = CsvParse({delimiter: ',', columns: true});
  parser.on('readable', function(){
    console.log('readable');
    let record;
    // eslint-disable-next-line no-cond-assign
    while (record = parser.read()) {
      console.log(record);
    }
  });
  const reader = new FileReader();
  reader.onload = (function() {
    return function(e) {
      console.log('Writing');
      parser.write(e.target.result);
    };
  })(f);

  reader.readAsText(f);
};

The input:

Letter,Number
A,1
B,2
C,3

The output:

Writing
readable
{Letter: "A", Number: "1"}
readable
{Letter: "B", Number: "2"}

Why is C3 not being read?
And why is "readable" being printed twice? I have a feeling I'm not understanding csv-parse correctly.

Thanks

Upvotes: 2

Views: 1173

Answers (1)

Votemike
Votemike

Reputation: 882

I was missing parser.end(); after parser.write(e.target.result); The last row was read correctly after adding that.

Upvotes: 6

Related Questions