Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsvWriter Mixin "TypeError: May not write null values to stream" #46

Open
bllevy opened this issue Jun 14, 2017 · 1 comment
Open

CsvWriter Mixin "TypeError: May not write null values to stream" #46

bllevy opened this issue Jun 14, 2017 · 1 comment

Comments

@bllevy
Copy link

bllevy commented Jun 14, 2017

I've been attempting to use the CsvWriter mixin to write a stream of Objects to a csv.

Upon writing the final record to the csv, the CsvWriter errors out (as described below).

TypeError: May not write null values to stream
    at validChunk (_stream_writable.js:184:10)
    at CsvTransformStream.Writable.write (_stream_writable.js:218:12)
    at Pump.<anonymous> (/Users/..../.../.../node_modules/datapumps/lib/mixin/CsvWriterMixin.js:27:35)
    at emitNone (events.js:91:20)
    at Pump.emit (events.js:185:7)
    at Pump.module.exports.Pump._outputBufferEnded (/Users/.../.../.../node_modules/datapumps/lib/Pump.js:233:19)
    at emitNone (events.js:86:13)
    at Buffer.emit (events.js:185:7)
    at Buffer.seal (/Users/.../.../.../node_modules/datapumps/lib/Buffer.js:149:14)
    at Pump.module.exports.Pump.sealOutputBuffers (/Users/.../.../.../node_modules/datapumps/lib/Pump.js:273:32)
    at Pump.module.exports.Pump._pump (/Users/.../.../.../node_modules

Here is the CsvWriter mixin code (as pulled from node_modules):

(function() {
  var CsvWriterMixin, Promise, csv, fs;

  csv = require('fast-csv');

  fs = require('fs');

  Promise = require('bluebird');

  CsvWriterMixin = function(options) {
    if (!(options != null ? options.path : void 0)) {
      throw new Error('path option is required.');
    }
    return function(target) {
      target.writeRow = function(row) {
        return target._csv.writer.writeAsync(row);
      };
      target._csv = options;
      target._csv.writer = Promise.promisifyAll(csv.createWriteStream());
      target._csv.writer.pipe(fs.createWriteStream(target._csv.path, {
        encoding: 'utf8'
      }));
      if (target._csv.headers != null) {
        target.writeRow(target._csv.headers);
      }
      return target.on('end', function() {
        return target._csv.writer.write(null);
      });
    };
  };

  module.exports = CsvWriterMixin;

}).call(this);

I've resolved the issue locally by updating this line of code:

      return target.on('end', function() {
        return target._csv.writer.write(null);
      });

to

      return target.on('end', function() {
        return target._csv.writer.end();
      });
@ORESoftware
Copy link

+1

djheru pushed a commit to djheru/node-datapumps that referenced this issue Jul 29, 2018
This relates to agmen-hu#46

It looks like the 'end' handler calls write(null) on the stream in the 'end' handler. That throws an uncaught error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants