node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

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

Open Minori-Akizuki opened this issue 4 years ago • 1 comments

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.

Minori-Akizuki avatar Apr 25 '20 11:04 Minori-Akizuki

@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;
    },
  };
};

guns2410 avatar Nov 04 '20 12:11 guns2410