Node readline module doesn't have 'on' function?

痴心易碎 提交于 2019-12-07 06:13:37

问题


I'm trying to create a node app that reads a textfile line by line using the 'readline' module, and prints it to the console.

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

According to the module's documentation, there should be an 'on' method. However when I log the instance of the readline object I created, I don't see an 'on' method anywhere:

{ 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] }

And so, naturally, when I call lineReader.on(), I get an error saying the function doesn't exist.

I'm following the documentation precisely... what am I missing? Where is the on method?

Thank you so much in advance for your time.


回答1:


Keep reading the docs until you find an example with context:

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 is a method of the interface returned by the createInterface method, not of the readline module itself.

  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);
  });



回答2:


You are trying to call the method on the module, not on the result of createInterface()

Instead of this:

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

try this:

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

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

Example:

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);
});

As you can see the .on() is called on the result of calling .createInterface() - not on the same object that the .createInterface() method was called on.



来源:https://stackoverflow.com/questions/43473537/node-readline-module-doesnt-have-on-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!