gpt4 book ai didi

javascript - Node.js "Cannot read property ' 原型(prototype)“未定义”错误 - Node.js、MongoDB 和 Angularjs 书

转载 作者:IT老高 更新时间:2023-10-28 13:33:54 27 4
gpt4 key购买 nike

我正在阅读 Brad Dayley 的 Node.js、MongoDB 和 Angularjs 一书,但我一直坚持他的一项练习( list 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')

关于javascript - Node.js "Cannot read property ' 原型(prototype)“未定义”错误 - Node.js、MongoDB 和 Angularjs 书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25196301/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com