簡體   English   中英

Node.js“無法讀取未定義的屬性‘原型’”錯誤 - Node.js、MongoDB 和 Angularjs 一書

[英]Node.js “Cannot read property 'prototype' of undefined” error - Node.js, MongoDB and Angularjs book

我正在閱讀 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

我在這里做錯了什么? 我應該如何調試這個,我應該在哪里看? 在此先感謝您幫助新手。

干杯。

process.EventEmitter已棄用,請改用require('events')

在所有參考文件(應該在多個文件中)中搜索:

var EventEmitter = process.EventEmitter

在它存在的地方,將該行更改為:

var EventEmitter = require('events')

我會把它作為答案發布,這樣這個問題就不會被標記為未回答。

你應該改變:

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

到:

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

var EventEmitter = process.EventEmitter在 node 的高版本中被棄用,使用var EventEmitter = require('events')代替

這個編譯錯誤是因為 npm 和 node.js 的版本沖突。 我在我的項目中遇到了同樣的問題。 我在安裝了與另一台機器上相同的兼容版本的 npm 和 node 后解決了這個問題。

查看:

nmp -v 

node -v.

與實際示例的所有混淆都在於第一行代碼。

 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;

幾分鍾前我遇到了同樣的問題,我決定將 node 降級到 6.x 版,然后它起作用了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM