简体   繁体   中英

MissingSchemaError: Schema hasn't been registered for model

How to reference another schema properly?

Error:

MissingSchemaError: Schema hasn't been registered for model "CategorySub".

Model file:

// 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

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.

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

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:

mongoose schema creation

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

It's not a MissingSchemaError , however.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM