Skip to content

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

@bllevy

Description

@bllevy

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();
      });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions