![](/img/trans.png)
[英]'Cannot read property 'collection' of undefined' in mongodb and node.js
[英]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.