Node.js Stream - Buffer to String gives [object Object]

I would like to be able to filter the output. However, I have a problem converting from buffer to string. console.log(JSON.stringify(obj.toString()));

keeps giving me [object Object]

which i can't use. How do I convert the buffer to a string so that I can filter the content to stdout?

//inject 'bower and javascript' files or just 'javascript' files
function injectStream(sourceStream, filesStream) {
    sourceStream
        .pipe(injector(filesStream, { ignorePath: 'app', addRootSlash: false }))
        .pipe(gulp.dest(INDEX_PATH_PARENT))
        .pipe(through2.obj(function(obj, enc, next) {
            console.log(JSON.stringify(obj.toString()));
            this.push(obj.contents);
            next();
    })).pipe(process.stdout)
}

      

+3


source to share


1 answer


through2.obj

creates a stream of objects (or a stream in object mode). Through a stream of objects, objects are a stream, not buffers. You are not getting a buffer, but an object obj

. That's why his method toString

gives [object Object]

. Perhaps what you are looking for is in obj.contents

?



+4


source







All Articles