【发布时间】: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