【发布时间】:2014-01-06 07:36:43
【问题描述】:
一段时间以来,我一直被以下错误困扰:
TypeError: Cannot read property 'scope' of undefined
at model.Object.defineProperty.set [as size] (/Users/sourabhdesai/Documents/nodejstuts/expressTest/node_modules/mongoose/lib/document.js:1200:58)
at Function.compile (/Users/sourabhdesai/Documents/nodejstuts/expressTest/node_modules/mongoose/lib/model.js:2516:24)
at Mongoose.model (/Users/sourabhdesai/Documents/nodejstuts/expressTest/node_modules/mongoose/lib/index.js:358:17)
at NativeConnection.Connection.model (/Users/sourabhdesai/Documents/nodejstuts/expressTest/node_modules/mongoose/lib/connection.js:600:23)
at Object.<anonymous> (/Users/sourabhdesai/Documents/nodejstuts/expressTest/user/index.js:87:20)
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 Module.require (module.js:364:17)
这个被抛出的行(user/index.js:87)是:
MusicQueueObj = db.model('MusicQueue', MusicQueueSchema);
我之前在下面的代码中定义了db:
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'musicqueuedb');
关于导致此错误的任何线索?我已经彻底搜索了 SO 和互联网以获取建议,但一无所获。我正在学习 node.js 和猫鼬,所以任何信息都只能在这一点上有益。我很确定我的架构设置正确。
这是我定义MusicQueueSchema的方式:
var MusicQueueSchema = new mongoose.Schema({
size : Number,
marker : Number,
array : [String]
});
下面是一些实例方法:
MusicQueueSchema.methods.doubleArray = function() {
var newArray = new Array( 2 * this.array.length );
for (var i = this.array.length - 1; i >= 0; i--) {
newArray[i] = this.array[i];
};
this.array = newArray;
};
MusicQueueSchema.methods.addSong = function(songLink) {
songLink = songLink.replace("%2F","/");
if( this.size == this.array.length ) this.doubleArray();
this.array[this.size] = obj;
this.size++;
};
MusicQueueSchema.methods.shuffle = function() {
var permArray = randperm(elems.length); // randperm(...) is a function I defined later on in this module. Its not a method of MusicQueueSchema
var newArray = new Array(permArray.length);
for (var i = elems.length - 1; i >= 0; i--) {
newArray[i] = this.array[permArray[i]];
}
};
这些并不是全部(这将是相当多的代码),但足以说明我是如何为此 MusicQueueSchema 编写所有实例方法的。
【问题讨论】:
-
您需要显示更多架构定义代码。
-
@WiredPrairie 我刚刚做了。谢谢!
-
好的 - 我刚刚尝试了基本模式,它工作正常。还有更多你没有展示的吗?
-
@WiredPrairie 你知道我刚刚发现了问题。当我为 size 方法注释掉我的代码时,它起作用了。我不知道为什么,它是一种非常简单的方法(而且非常不必要,所以我可以删除它)。就是这样:
MusicQueueSchema.methods.size = function() { return this.size; }; -
哦,好吧,我知道为什么了。我会发布我自己问题的答案。
标签: javascript node.js mongodb express mongoose