Skip to content Skip to sidebar Skip to footer

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 = requi

Solution 1:

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.


Post a Comment for "MissingSchemaError: Schema Hasn't Been Registered For Model"