简体   繁体   中英

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

I am would like to be able to filter the output. However, I am having a issue converting from buffer to string. console.log(JSON.stringify(obj.toString())); keeps giving me [object Object] which I can not use. How can I convert the buffer to string so I can filter out the contents 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)
}

through2.obj makes an object stream (or a stream in object mode). Through an object stream objects flow, not buffers. What you get is not a buffer, but an object obj . That is why its toString method gives [object Object] . Perhaps what you are looking for is in obj.contents ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM