Keyang / node-csvtojson

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
MIT License
2.02k stars 271 forks source link

when it try to read twice file that `fromFile` will happen `Uncaught Promise Error` #383

Open Minori-Akizuki opened 4 years ago

Minori-Akizuki commented 4 years ago

when I use fromFile to read CSV file. In the application some time happened read same file because its interactive program.

its simple code.

module.exports = function() {
  return {
    readcsv: async function(path) {
      const json = await csvReader.fromFile(path);
      return json;
    },
  };
};

I had this error

[2020-04-25 19:49:31] Uncaught Promise Error: 
[2020-04-25 19:49:31] Error [ERR_STREAM_WRITE_AFTER_END]: write after end
[2020-04-25 19:49:31]     at writeAfterEnd (_stream_writable.js:265:14)
[2020-04-25 19:49:31]     at Converter.Writable.write (_stream_writable.js:314:5)
[2020-04-25 19:49:31]     at ReadStream.ondata (_stream_readable.js:695:22)
[2020-04-25 19:49:31]     at ReadStream.emit (events.js:310:20)
[2020-04-25 19:49:31]     at addChunk (_stream_readable.js:286:12)
[2020-04-25 19:49:31]     at readableAddChunk (_stream_readable.js:268:9)
[2020-04-25 19:49:31]     at ReadStream.Readable.push (_stream_readable.js:209:10)
[2020-04-25 19:49:31]     at internal/fs/streams.js:210:12
[2020-04-25 19:49:31]     at FSReqCallback.wrapper [as oncomplete] (fs.js:488:5)

maybe, I wrong to use promise. if it is true, please close this issue.

Thank you.

guns2410 commented 4 years ago

@Minori-Akizuki What you need is to execute the csvReader function for each file. something like this:

const csvReader = require('csvtojson') // and not const csvReader = require('csvtojson')()
module.exports = function() {
  return {
    readcsv: async function(path) {
      const json = await csvReader().fromFile(path);
      return json;
    },
  };
};