In my models/user.js
file:
var mongoose = require(\'mongoose\');
var Schema = mongoose.Schema;
var userSchema = new Schema({
(define schema
I literally research lot and I found a solution so, I share this solution to you so, no one can face that cheap mistake that I did.
Please remember, you just need to add the same name in ref
as you gave in your model
because its case sensitive ( Product !== product ).
const Product = new mongoose.model('Product', productSchema);
product: {
type: mongoose.Schema.ObjectId,
ref: 'Product', <-- Should be same as modelName
required: [true, 'Cart must belong to a product']
}
In my case, it happened because of capital/small letter confusion. User model had this:
const userSchema = new Schema({
// ...
});
module.exports = mongoose.model('User', userSchema);
And Product model had a reference to User model but small case:
const productSchema = new Schema({
// ...
userId: {
type: Schema.Types.ObjectId,
ref: 'user', // This should exactly match the name of User model above!!!
required: true
}
});
I was making a different mistake like I have created the Schema but the issue was that I didn't use schema model (for insert, update, read and delete the doc).
i.e. I have created the collection something like that:
const playerSchema = new Schema({
bowling: {
style: String,
arm: String,
}
})
export const PlayerSchema = model('Player', playerSchema)
but didn't use or call PlayerSchema
model somewhere that's why I was getting this error.
I've also experienced this error with ES6/Typescript. Even I imported the model, the error still persisted. According to docs here
MongooseError.MissingSchemaError
Thrown when you try to access a model that has not been registered yet
import { model, Schema } from 'mongoose';
import Company from './CompanyModel';
const ProjectSchema = new Schema({
company: { type: Schema.Types.ObjectId, ref: "Company" }
});
export default model('Project', ProjectSchema);
The tips was just to make sure to use the model explicitly, so changing ref:"Company"
into ref:Company.modelName
seemed fixed it.
I hope that helps some of you
Ex.
In the below example, you will get MissingSchemaError: Schema hasn't been registered for model “Role”
if you don't do const Role = require("./role");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Role = require("./role");
const userSchema = new Schema(
{
role: { type: Schema.Types.ObjectId, ref: "Role", required: false },
username: { type: String, required: true, trim: true },
password: { type: String, required: true, trim: true },
email: { type: String, required: true, trim: true }
},
{ timestamps: true }
);
module.exports = mongoose.model("User", userSchema);
I recently came across a solution similar to the answers provided by @rajkumar-nagarajan and @verybadalloc.
I like this approach better, because it avoids the n+1 iteration, and it does not read all the models from the file system.
In app/models/index.js
, require each model file:
require('./model1');
require('./model2');
require('./model3');
Then, in /app.js
or /index.js
(whichever your project uses), you can now simply do:
require('./app/models');
The part I'm missing (any tips appreciated here) --how to easily register each model by name like:
mongoose.model('Model1')