Node.js EventEmitter error

萝らか妹 提交于 2019-12-02 12:27:44

A couple of things. You are overwriting the prototype rather than extending it. Also, move the util.inherits() call before you add the new method:

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var Consumer = function Consumer() {}

util.inherits(Consumer, EventEmitter);

Consumer.prototype.findById = function(id) {
    this.emit('done', this);
    console.log('found');
};

var c = new Consumer();
c.on('done', function(result) {
  console.log(result);
});

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