WriteStream.emit错误:这段代码我在做什么错

时间:2019-05-09 10:09:59

标签: node.js

我正在学习nodeJs。我正在学习关于nodejs中流的知识。 我正在尝试使用writestream写入文件,一旦写入完成,我将尝试使用readstream读取同一文件。写入文件的代码无法流畅地工作,但是当我尝试在writestream的“ finish”事件中读取文件时,它给我一个错误。 这是我的代码:

writerstream.on('finish', function(){
    console.log('write complete');
    var readerstream = fs.ReadStream('output.txt');
    var data2 = '';
    readerStream.setEncoding('UTF8');

    readerstream.on('data', function(chunk){
        data2+=chunk;
    });

    readerstream.on('end', function(){
     console.log('Test Message');
     console.log(data2);       
    });
});

请告诉我我在做什么错。它确实会帮助我学习得更好。

错误:

ReferenceError: readerStream is not defined
at WriteStream.<anonymous> (D:\D\PV\NodeJs\Second.js:47:5)
at emitNone (events.js:111:20)
at WriteStream.emit (events.js:208:7)
at finishMaybe (_stream_writable.js:614:14)
at afterWrite (_stream_writable.js:465:3)
at onwrite (_stream_writable.js:455:7)
at fs.write (fs.js:2192:5)
at FSReqWrap.wrapper [as oncomplete] (fs.js:685:5))

0 个答案:

没有答案