Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

340
Vistas
Not able to stream object array from transform stream nodejs

I am trying to read an array of objects, transform them and write to a file in Nodejs, it is giving me error "The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object"

My demo Code:

const readStream = stream.Readable.from(res); // I create stream from array of objects
This is my custom transform stream:
var Transform = stream.Transform;

function Flatten(options) {
  Transform.call(this, options);
};
util.inherits(Flatten, Transform);
Flatten.prototype._transform = function (chunk, enc, cb) {
  
    let obj = chunk;
    let obj2;
    for (let j=0; j<obj.inner.length; j++) {
        let obj1 = obj.inner[j];
        obj2 = {...obj, ...obj1};
        
        delete obj2.inner;
        this.push(new Buffer(obj2));
    }
    
  cb();
};
Then I have a writable stream:
let writeStream = fs.createWriteStream('test1.txt');

Then I pipe all these await pipeline(readStream, tranf, writeStream);
that time I am getting the above error, tried using objectMode: true.


Could you please help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

first of all you need to add an option to work with object objectMode: true. then instead of passing Buffer to this.push() change to string with JSON.stringify

const stream = require('stream');
const fs = require('fs');
const res = [{ inner: [1, 2, 3] }, { inner: [4, 5, 6] }, { inner: [7, 8, 9] }];
const Transform = stream.Transform;

class Flatten extends Transform {
    constructor(options) {
        super(options)
    }

    _transform(chunk, enc, cb) {
        let obj = chunk;
        let obj2;
        for (let j = 0; j < obj.inner.length; j++) {
            let obj1 = obj.inner[j];
            obj2 = { ...obj, ...obj1 };
            delete obj2.inner;
            this.push(JSON.stringify(obj2));
        }
        cb();
    };

};


const readStream = stream.Readable.from(res);
const writeStream = fs.createWriteStream('test1.txt');
const transform = new Flatten({ objectMode: true })

readStream.pipe(transform).pipe(writeStream);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda