【问题标题】:Node readline module doesn't have 'on' function?节点 readline 模块没有“on”功能?
【发布时间】:2017-09-14 08:57:27
【问题描述】:

我正在尝试创建一个节点应用程序,它使用“readline”模块逐行读取文本文件,并将其打印到控制台。

  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”方法:

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

因此,很自然地,当我调用lineReader.on() 时,我收到一条错误消息,指出该函数不存在。

我正在精确地遵循文档......我错过了什么? on 方法在哪里?

非常感谢您抽出宝贵时间。

【问题讨论】:

    标签: javascript node.js methods module filereader


    【解决方案1】:

    继续阅读文档,直到找到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()) {
      // …
    

    oncreateInterface 方法返回的接口 的方法,而不是readline 模块本身的方法。

      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】:

    你试图调用模块上的方法,而不是createInterface()的结果

    而不是这个:

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

    试试这个:

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

    请参阅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() 方法的同一个对象上。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2019-06-01
      • 2015-04-11
      • 2020-07-02
      相关资源
      最近更新 更多