MissingSchemaError:尚未为模型注册架构 [英] MissingSchemaError: Schema hasn't been registered for model

查看:97
本文介绍了MissingSchemaError:尚未为模型注册架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确引用其他模式?

How to reference another schema properly?

错误:

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);


编辑


EDIT

这是我接手的一个小项目,是MongoDB/Mongoose的新手.我是在前任所有者的app.js中找到的:

This is a small project I took over, and I'm new to MongoDB/Mongoose. I found this in app.js from the previous owner:

//load models
var models_path = __dirname + '/models/'
fs.readdirSync(models_path).forEach(function (file) {
  if(~file.indexOf('.js')){
    require(models_path + '/' + file);
  }
})

它只是遍历该文件夹并逐个注册每个架构.但是,在该文件夹中,我的子模式位于我的父模式之前,因此首先要注册它.

It simply goes through the folder and registers each schema one by one. However, in the folder my child schema is before my parent schema, so it's getting registered first.

我添加了一个如下所示的models.js文件:

I added a models.js files that looks like this:

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]);
    }
};

然后替换app.js中的初始代码以调用需要此新模型文件的文件,如下所示:

And then replaced the initial code in app.js to call require this new models file like so:

require('./models/models.js').initialize();

我从这个受欢迎的问题的答案之一中得到了这个主意:

I got this idea from one of the answers in this popular question:

猫鼬模式创建

但是,现在我从我的category_sub.js模型文件中得到了一个ReferenceError: CategoryMainSchema is not defined.

However, now I'm getting a ReferenceError: CategoryMainSchema is not defined coming from my category_sub.js model file.

但是,它不是MissingSchemaError.

推荐答案

未定义您的架构以防万一.您的model.js和app.js只是简单地遍历了您定义的每个模型.这与在CategoryProduct的模型文件中具有CategorySubSchema的可见性不同.

Your schema is not defined in case. Your model.js and app.js is simply going through each of the models that you have defined. That's different from having visibility of CategorySubSchema in your CategoryProduct's model file.

在NodeJS中,您定义的架构和模型不会全局范围内.您假设方案在全局可见时,您遇到的问题是范围问题.您必须将它们导出,其他功能才能看到它们.

In NodeJS, the schema and model you defined won't be globally scoped. The problem you are having is the scoping issue as you are assuming the Schemas are globally visible. You will have to export them for the other functions to see them.

请在此处引用nodejs module_export链接: http://nodejs.org/api/modules. html#modules_module_exports

Please reference the nodejs module_export link here: http://nodejs.org/api/modules.html#modules_module_exports

在您的情况下,您可能需要将代码更改为模型文件中的以下代码:

In your case, you probably need to change your code to the follow in your model file:

exports.CategoryMainSchema = CategoryMainSchema; 

每个模型文件中的

等.然后,在您要使用这些模型作为子方案的模型文件中,可以要求使用以下模型:

and so on in each model file. Then in the model files you want to use these as subschemas, you can require that model like:

var CategoryMainSchema = require('CategoryMain').CategoryMainSchema //assuming CategoryMain is the name of your model file

语法可能有点错误,但是请尝试一下.谢谢.

The syntax might be a bit off but please give it a try. Thanks.

这篇关于MissingSchemaError:尚未为模型注册架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆