【发布时间】:2022-06-15 14:20:55
【问题描述】:
我正在尝试读取对象数组,将它们转换并写入 Nodejs 中的文件,它给了我错误““块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例。收到一个实例对象”
我的演示代码:
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.
你能帮忙吗
【问题讨论】:
标签: javascript node.js stream nodejs-stream