[英]MEAN.JS MissingSchemaError
我正在與MEAN.JS一起玩,以了解自己的喜好,並且遇到了通常可以解決的錯誤,但是這次我似乎無法弄清楚自己在做什么錯。
我正在嘗試使用貓鼬的populate方法填充子對象,但是現在出現此錯誤: MissingSchemaError: Schema hasn't been registered for model "topic"
這很有意義……請確保“ topic”模型架構已加載。 我認為應該根據MEAN.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
'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);
導致錯誤的查詢:
Moment.find().sort('-created').populate('user', 'displayName', 'topic').exec(function(err, moments) { ... }
是什么導致此錯誤,如何解決? 我之前在其他節點系統中已經解決了這個問題,但是在meanjs中,我認為我缺少基本的東西。
弄清楚了。 我一直在忽略如何正確使用填充。 為了解決這個問題,我只是將另一個填充調用鏈接到另一個dbRef值,如下所示:
Moment.find()
.sort('-created')
.populate('user', 'displayName')
.populate('topic')
.exec(function(err, moments) {
// do stuff with results
});
現在,將填充主題以及用戶名。
剛剛給我自己寫了一個便箋,標題為:RTFM。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.