Error: MissingSchemaError: Schema hasn't been registered for model "". Use mongoose.model(name, schema)
If you are getting the following error
Error: MissingSchemaError: Schema hasn't been registered for model "<Model>". Use mongoose.model(name, schema)
and do not know how to resolve then let me help you.
Check into all your models, if you have by mistake added a reference object to the Model the error is mentioning. Ctrl+F on your models and you will figure out the place where a non-existing model is being referenced.
Not quite your fault. This error is not really good at explaining itself. Something which MongoDB, Mongoose guys must work upon.
I had got the following error:
Error: MissingSchemaError: Schema hasn't been registered for model "Comment". Use mongoose.model(name, schema)
Here is how my model looked. Instead of referncing to User, I referenced it to Comments.
Error: MissingSchemaError: Schema hasn't been registered for model "<Model>". Use mongoose.model(name, schema)
and do not know how to resolve then let me help you.
Check into all your models, if you have by mistake added a reference object to the Model the error is mentioning. Ctrl+F on your models and you will figure out the place where a non-existing model is being referenced.
Not quite your fault. This error is not really good at explaining itself. Something which MongoDB, Mongoose guys must work upon.
I had got the following error:
Error: MissingSchemaError: Schema hasn't been registered for model "Comment". Use mongoose.model(name, schema)
Here is how my model looked. Instead of referncing to User, I referenced it to Comments.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const productSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
comments: [
{
userId: {
type: Schema.Types.ObjectId,
ref: 'Comments',
required: true
},
message: {
type: String,
required: true
}
}
]
}, {timestamps: true});
Comments
Post a Comment