Javascript MissingSchemaError:尚未为模型注册架构
如何正确引用另一个模式 错误:Javascript MissingSchemaError:尚未为模型注册架构,javascript,mongoose,Javascript,Mongoose,如何正确引用另一个模式 错误: MissingSchemaError: Schema hasn't been registered for model "CategorySub". 模型文件: // module dependencies var mongoose = require('mongoose') , Schema = mongoose.Schema , CategoryMain = mongoose.model('CategoryMain') , CategorySub
MissingSchemaError: Schema hasn't been registered for model "CategorySub".
模型文件:
// module dependencies
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, CategoryMain = mongoose.model('CategoryMain')
, CategorySub = mongoose.model('CategorySub');
// set up the schema
var CategoryProductSchema = new Schema({
name: { type: String },
_category_main : [CategoryMainSchema],
_category_sub : [CategorySubSchema]
},
{
collection: 'categories_product'
}
)
// before save function equivalent
CategoryProductSchema.pre('save', function(next){
var now = new Date();
this.updated_at = now;
if ( !this.created_at ) {
this.created_at = now;
}
next();
})
CategoryProductSchema.set('toObject', { getters: true });
mongoose.model('CategoryProduct', CategoryProductSchema);
exports.CategoryMainSchema = CategoryMainSchema;
编辑
这是我接手的一个小项目,我是MongoDB/Mongoose的新手。我在app.js中从以前的所有者那里找到了这个:
//load models
var models_path = __dirname + '/models/'
fs.readdirSync(models_path).forEach(function (file) {
if(~file.indexOf('.js')){
require(models_path + '/' + file);
}
})
它只是遍历文件夹并逐个注册每个模式。但是,在文件夹中,我的子模式在我的父模式之前,因此首先要注册它
我添加了一个models.js文件,如下所示:
var models = ['token.js',
'user.js',
'category_main.js',
'category_sub.js',
'category_product.js',
'product.js'
];
exports.initialize = function() {
var l = models.length;
for (var i = 0; i < l; i++) {
require(models[i]);
}
};
我从这个流行问题的一个答案中得到了这个想法:
然而,现在我得到了一个ReferenceError:CategoryMainSchema不是从我的category_sub.js模型文件中定义的
然而,这不是一个丢失的模式错误。您的模式在case中没有定义。您的model.js和app.js只是浏览您定义的每个模型。这不同于在CategoryProduct的模型文件中具有CategorySubSchema的可见性 在NodeJS中,您定义的模式和模型将不具有全局范围。您面临的问题是范围问题,因为您假设模式是全局可见的。您必须导出它们,其他函数才能看到它们 请参考此处的nodejs模块导出链接: 在您的情况下,您可能需要在模型文件中将代码更改为以下内容:
// module dependencies
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, CategoryMain = mongoose.model('CategoryMain')
, CategorySub = mongoose.model('CategorySub');
// set up the schema
var CategoryProductSchema = new Schema({
name: { type: String },
_category_main : [CategoryMainSchema],
_category_sub : [CategorySubSchema]
},
{
collection: 'categories_product'
}
)
// before save function equivalent
CategoryProductSchema.pre('save', function(next){
var now = new Date();
this.updated_at = now;
if ( !this.created_at ) {
this.created_at = now;
}
next();
})
CategoryProductSchema.set('toObject', { getters: true });
mongoose.model('CategoryProduct', CategoryProductSchema);
exports.CategoryMainSchema = CategoryMainSchema;
在每个模型文件中依次类推。然后,在要将其用作子模式的模型文件中,可以要求该模型,如:
var CategoryMainSchema = require('CategoryMain').CategoryMainSchema //assuming CategoryMain is the name of your model file
语法可能有点错误,但请尝试一下。谢谢 我认为首先需要定义模式——对于CategoryMainSchema和CategorySubSchema,或者在上面,或者链接到已经定义它们的地方。