我在读取流上附加'data'事件处理程序时遇到错误。 如果我把这个处理程序放在代码中提到的其他地方(管道之前),我没有看到任何异常。
请帮助我,这里有什么问题?
var Readable = require("stream").Readable;
var readStream = new Readable;
readStream.on('data',function(chunk){console.log(chunk);}); //Error
readStream.push('first data');
readStream.push('second data');
readStream.push(null);
//if I place data event handler here, no Error is seen
readStream.pipe(process.stdout);
//绑定数据事件处理程序时出错
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.<anonymous> (_stream_readable.js:745:45)
at Readable.EventEmitter.emit (events.js:92:17)
at emitDataEvents (_stream_readable.js:771:10)
at Readable.on (_stream_readable.js:692:5)
at Object.<anonymous> (/home/pk/node/readableStream.js:12:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
答案 0 :(得分:6)
您无法简单地实例化Readable
流,因为它没有实现有效的_read
方法。如果您只想将一些数据推送到它(不执行实际IO),只需将其设置为noop:
readStream._read = function () {};