【问题标题】:Node.js "Cannot read property 'prototype' of undefined" error - Node.js, MongoDB and Angularjs bookNode.js“无法读取未定义的属性‘原型’”错误 - Node.js、MongoDB 和 Angularjs 书
【发布时间】:2020-11-13 01:43:24
【问题描述】:

我正在阅读 Brad Dayley 的 Node.js、MongoDB 和 Angularjs 一书,但我一直坚持他的一项练习(清单 4.4)。我有一个简单的脚本emitterListener.js,如下所示,该脚本旨在对帐户进行检查。

var events = require('events');
function Account() {
    this.balance = 0;
    events.EventEmitter.call(this);
    this.deposit = function(amount) {
        this.balance += amount;
        this.emit('balanceChanged');
    };
    this.withdraw = function(amount) {
        this.balance -= amount;
        this.emit('balanceChanged');
    };
}
Account.prototype._proto_ = events.EventEmitter.prototype;

function displayBalance() {
    console.log("Account balance: $%d", this.balance);
}

function checkOverdraw() {
    if (this.balance < 0) {
        console.log("Account Overdrawn!!!!!");
    }
}

function checkGoal(acc, goal) {
    if (acc.balance > goal) {
        console.log("Goal Achieved!!!!!");
    }
}
var account = new Account();

account.on("balanceChanged", displayBalance);
account.on("balanceChanged", checkOverdraw);
account.on("balanceChanged", function() {
    checkGoal(this, 1000);
});
account.deposit(220);
account.deposit(320);
account.deposit(600);
account.withdraw(1200);

当我运行它时,我得到了错误。

TypeError: Object #<Account> has no method 'on'
at Object.<anonymous> (/Users/506132/web/emitterListener.js:35:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

根据我在研究后的有限理解,我认为这意味着“on”模块没有被加载。我找到了一个建议类似于将其添加到第 1 行的解决方案

var events = require('events').on;

这会导致错误。

TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

按照第一个修复的逻辑,我尝试使用 EventEmitter 实现相同的修复

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

万岁,它看起来像它工作......或者没有......现在我得到这个错误

TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:17:48)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

我尝试添加下面的代码,为什么不呢?

var events = require('events').prototype;

它只是让我回到以前的相同错误

TypeError: Cannot read property 'EventEmitter' of undefined
at Object.<anonymous> (/Users/506132/web/emitterListener.js:16:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

我在这里做错了什么?我应该如何去调试这个,我应该去哪里看?提前感谢您帮助新手。

干杯。

【问题讨论】:

  • 如果你改变:'Account.prototype._proto_ = events.EventEmitter.prototype;'到:'Account.prototype。 = Object.create(events.EventEmitter.prototype);' 这有帮助吗?让导入保持原样,因为添加 .on 只会破坏它。更多关于原型的信息:stackoverflow.com/questions/16063394/…
  • 看起来你可能还需要.require('events').EventEmitter:nodejs.org/api/events.html

标签: javascript node.js angularjs mongodb


【解决方案1】:

process.EventEmitter 已弃用,请改用require('events')

在所有参考文件(应该在多个文件中)搜索:

var EventEmitter = process.EventEmitter

只要它存在,就把那行改成这样:

var EventEmitter = require('events')

【讨论】:

    【解决方案2】:

    我会把它作为答案发布,这样这个问题就不会被标记为未回答。

    你应该改变:

    Account.prototype.__proto__ = events.EventEmitter.prototype;
    

    到:

    Account.prototype = Object.create(events.EventEmitter.prototype);
    

    【讨论】:

      【解决方案3】:

      var EventEmitter = process.EventEmitter 在高版本节点中已弃用,请改用var EventEmitter = require('events')

      【讨论】:

        【解决方案4】:

        这个编译错误是因为npm和node的版本冲突。我在我的项目中遇到了同样的问题。我在安装与另一台机器上相同的兼容版本的 npm 和 node 后解决了。

        检查:

        nmp -v 
        

        node -v.
        

        【讨论】:

          【解决方案5】:

          与实际示例的所有混淆都在于第一行代码。

           var events = require('events');
          

          Node v6.1.0之后,这行代码会将EventEmmiter对象存储在events变量中,以后就不需要显式访问了。

          解决方法很简单,改一下就行了

          events.EventEmitter.call(this);
          

          到:

          events.call(this)
          

          然后改变

          Account.prototype._proto_ = events.EventEmitter.prototype;
          

          到:

          Account.prototype._proto_ = events.prototype;
          

          【讨论】:

            【解决方案6】:

            几分钟前我遇到了同样的问题,我决定将节点降级到版本 6.x,然后它工作了

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-03-24
              • 2019-12-01
              • 1970-01-01
              • 2019-07-28
              • 1970-01-01
              • 2015-05-11
              • 2012-10-03
              • 1970-01-01
              相关资源
              最近更新 更多