2017-04-18 8 views
0

私は 'readline'モジュールを使って行ごとにテキストファイルを読み込んでコンソールに出力するノードアプリケーションを作成しようとしています。ノードreadlineモジュールに 'on'機能がありませんか?

var lineReader = require('readline'); 
    lineReader.createInterface({ 
    input: fs.createReadStream('./testfile') 
    }); 
    lineReader.on('line', function(line){ 
    console.log(line); 
    }); 

there should be an 'on' methodによると、しかし、私は私が作成したreadlineのオブジェクトのインスタンスをログインしたときに、私はどこかの方法「on」に表示されていない:私はlineReader.on()を呼び出すとき

{ createInterface: [Function], Interface: { [Function: Interface] 
    super_: 
     { [Function: EventEmitter] 
     EventEmitter: [Circular], 
     usingDomains: false, 
     defaultMaxListeners: [Getter/Setter], 
     init: [Function], 
     listenerCount: [Function] } }, 
emitKeypressEvents: [Function: emitKeypressEvents], 
cursorTo: [Function: cursorTo], 
moveCursor: [Function: moveCursor], 
clearLine: [Function: clearLine], 
clearScreenDown: [Function: clearScreenDown], 
codePointAt: [Function: deprecated], 
getStringWidth: [Function: deprecated], 
isFullWidthCodePoint: [Function: deprecated], 
stripVTControlCharacters: [Function: deprecated] } 

だから、当然、私はエラーを取得しますその機能は存在しないと言っています。

私はドキュメントに正確に従っています...何が欠けていますか?オンメソッドはどこですか?

お時間をいただきありがとうございます。あなたがan example with contextを見つけるまで

答えて

2

は、ドキュメントを読んでください:

var readline = require('readline'), 
    rl = readline.createInterface(process.stdin, process.stdout); 

rl.setPrompt('OHAI> '); 
rl.prompt(); 

rl.on('line', function(line) { 
    switch(line.trim()) { 
    // … 

onのreadlineモジュール自体のcreateInterface方法、ないで返さインタフェースの方法です。

var readline = require('readline'); 
    var lineReader = readline.createInterface({ 
    input: fs.createReadStream('./testfile') 
    }); 
    lineReader.on('line', function(line){ 
    console.log(line); 
    }); 

参照:これを試して

var lineReader = require('readline'); 
    lineReader.createInterface({ 
    input: fs.createReadStream('./testfile') 
    }); 
    lineReader.on('line', function(line){ 
    console.log(line); 
    }); 

:あなたがいない代わりに、こののcreateInterface()

の結果に、モジュール上のメソッドを呼び出すようにしようとしている

var lineReader = require('readline'); 

    // You need to capture the return value here 
    var foo = lineReader.createInterface({ 
    input: fs.createReadStream('./testfile') 
    }); 

    // … and then use **that** 
    foo.on('line', function(line){ 
    console.log(line); 
    }); 
+0

ありがとうございます – SemperCallide

1

docs at http://node.readthedocs.io/en/latest/api/readline/

例:

var readline = require('readline'), 
    rl = readline.createInterface(process.stdin, process.stdout); 

rl.setPrompt('OHAI> '); 
rl.prompt(); 

rl.on('line', function(line) { 
    switch(line.trim()) { 
    case 'hello': 
     console.log('world!'); 
     break; 
    default: 
     console.log('Say what? I might have heard `' + line.trim() + '`'); 
     break; 
    } 
    rl.prompt(); 
}).on('close', function() { 
    console.log('Have a great day!'); 
    process.exit(0); 
}); 

あなたは.on().createInterface()を呼び出した結果で呼び出され見ることができるように - ない.createInterface()メソッドがコールされた同じオブジェクトに。

関連する問題