[英]MEAN.JS MissingSchemaError
I'm playing with MEAN.JS to see how I like it and I got an error that I can usually solve, but this time I can't seem to figure out what I may be doing wrong. 我正在与MEAN.JS一起玩,以了解自己的喜好,并且遇到了通常可以解决的错误,但是这次我似乎无法弄清楚自己在做什么错。
I'm trying to populate a child object using mongooses populate method, but I'm now getting this error: MissingSchemaError: Schema hasn't been registered for model "topic"
This makes sense ... makes sure the "topic" model schema is loaded. 我正在尝试使用猫鼬的populate方法填充子对象,但是现在出现此错误:
MissingSchemaError: Schema hasn't been registered for model "topic"
这很有意义……请确保“ topic”模型架构已加载。 I thought it should be loaded according to the loading order in MEAN.js 我认为应该根据MEAN.js中的加载顺序进行加载
moment.server.model.js moment.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Moment Schema
*/
var MomentSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Moment name',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
created: {
type: Date,
default: Date.now
},
topic: {
type: Schema.ObjectId,
ref: 'Topic'
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Moment', MomentSchema);
topic.server.model.js topic.server.model.js
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
moment = require('moment-timezone');
/**
* Topic Schema
*/
var TopicSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Topic name',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
moments: [
{
type: Schema.ObjectId,
ref: 'Moment'
}
]
});
mongoose.model('Topic', TopicSchema);
The Query that causes the error: 导致错误的查询:
Moment.find().sort('-created').populate('user', 'displayName', 'topic').exec(function(err, moments) { ... }
What might be causing this error and how can I fix it? 是什么导致此错误,如何解决? I've solved this before in other node systems but in meanjs I think I'm missing something fundamental.
我之前在其他节点系统中已经解决了这个问题,但是在meanjs中,我认为我缺少基本的东西。
Figured it out. 弄清楚了。 I was ignoring how to use populate correctly.
我一直在忽略如何正确使用填充。 To fix, I simply chained another populate call to the other dbRef value as such:
为了解决这个问题,我只是将另一个填充调用链接到另一个dbRef值,如下所示:
Moment.find()
.sort('-created')
.populate('user', 'displayName')
.populate('topic')
.exec(function(err, moments) {
// do stuff with results
});
Now the topic as well as the username is populated. 现在,将填充主题以及用户名。
Just wrote a sticky note to my self entitled: RTFM. 刚刚给我自己写了一个便笺,标题为:RTFM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.