NodeJS:尝试推动可读性抛出“错误:未实现”
为什么下面的代码(保存为dummy.js
)会抛出一个错误? 什么是修复?
var Stream = require('stream'); var src = new Stream.Readable(); var dest = new Stream(); dest.writable = true; dest.write = function(data) { console.log("dest:",data); }; ["1","2","3"].forEach(function(f) { console.log(f); src.push(f); }); src.pipe(dest);
执行:
> node dummy.js 1 2 3 events.js:72 throw er; // Unhandled 'error' event ^ Error: not implemented at Readable._read (_stream_readable.js:446:22) at Readable.read (_stream_readable.js:320:10) at Readable.on (_stream_readable.js:701:14) at Readable.pipe (_stream_readable.js:556:10) at Object.<anonymous> (/home/jay/dummy.js:16:5) at (etc.)
在推送数组值之后和调用pipe
之前,按null
。 还需要添加dest.end = function(data) {};
在data.write = ...
。