Javascript Node.js";无法读取属性';原型&x27;“未定义”的定义;错误-Node.js、MongoDB和Angularjs手册
我正在阅读Brad Dayley的Node.js、MongoDB和Angularjs的书,我一直在做他的一个练习(清单4.4)。我有一个简单的脚本emitterListener.js,如下所示。该脚本旨在对帐户进行检查Javascript Node.js";无法读取属性';原型&x27;“未定义”的定义;错误-Node.js、MongoDB和Angularjs手册,javascript,node.js,angularjs,mongodb,Javascript,Node.js,Angularjs,Mongodb,我正在阅读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(a
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
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
万岁,看起来好像成功了。。。。或者不是……现在我得到了这个错误
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
这让我回到了以前的错误
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
TypeError:无法读取未定义的属性“EventEmitter”
反对。(/Users/506132/web/emitterListener.js:16:35)
在模块处编译(Module.js:456:26)
在Object.Module.\u extensions..js(Module.js:474:10)
在Module.load(Module.js:356:32)
在Function.Module.\u加载(Module.js:312:12)
位于Function.Module.runMain(Module.js:497:10)
启动时(node.js:119:16)
在node.js:906:3
我做错了什么?我应该如何调试它,我应该在哪里查找?提前感谢你帮助一个新手
干杯。我将把它作为答案发布,这样这个问题就不会被标记为未回答 您应该更改:
Account.prototype.__proto__ = events.EventEmitter.prototype;
致:
process.EventEmitter
已被弃用,请改用require('events')
在所有参考文件(应在多个文件中)中搜索:
如果存在,请将该行更改为:
var EventEmitter = require('events')
var EventEmitter=process.EventEmitter
如果在高版本的node中不推荐使用,请使用
var EventEmitter=require('events')
代替所有与实际示例混淆的都是第一行代码
var events = require('events');
在NodeV6.1.0之后,这行代码将在events变量中存储EventMeter对象,因此以后无需显式访问它
解决方案很简单,只需改变
events.EventEmitter.call(this);
致:
改变
Account.prototype._proto_ = events.EventEmitter.prototype;
致:
此编译错误是因为npm和节点的版本冲突。我的项目中也有同样的问题。在另一台机器上安装相同兼容版本的npm和node后,我解决了这个问题 检查:
nmp -v
及
几分钟前我也遇到了同样的问题,我决定将node降级到6.x版本,如果您更改:“Account.prototype.\u proto=events.EventEmitter.prototype;”,它就可以工作了致:'Account.prototype.=创建(events.EventEmitter.prototype);'这有用吗?让导入保持原样,因为添加.on只会破坏导入。关于prototype的更多信息:看起来您可能需要。require('events')。EventEmitter以及:
var EventEmitter = require('events')
var events = require('events');
events.EventEmitter.call(this);
events.call(this)
Account.prototype._proto_ = events.EventEmitter.prototype;
Account.prototype._proto_ = events.prototype;
nmp -v
node -v.