Last day of 2021! After spending just over 5 hours scouring the internet I've resorted to posting for help.
Desired Behaviour:
Attempted Resolutions:
Error
MissingSchemaError: Schema hasn't been registered for model "progressions".
Use mongoose.model(name, schema)
Note that this error is thrown when trying to populate "lesson" as well
Progress Model
const Progress = mongoose.model('Progress', new Schema({
lesson: {
type: Schema.Types.ObjectId,
ref: 'lesson'
},
progressions: [{
type: Schema.Types.ObjectId,
ref: 'progression'
}],
progress:{
type: Number
},
user:{
type: Schema.Types.ObjectId,
ref: 'user'
}
},{
timestamps: true,
}));
module.exports = Progress;
Progress Routes File
router.get('/all', async (req, res) => {
let user = await User.find({email:req.query.user})
let data = await Progress.find({user:user}).populate('progressions')
return res.status(201).json({
data,
message: 'data populated',
success: true
});
});
Progression Model
const Progression = mongoose.model('Progression', new Schema({
type:{
type:String
},
toBeReviewed:{
type: Date,
default: Date.now
},
},{
timestamps: true,
}));
module.exports = Progression;
Try to change the progressions property ref attribute in the Progress schema to match the Progression model name (capitalize it):
progressions: [{
type: Schema.Types.ObjectId,
ref: 'Progression'
}]
All code in your mongoose files should have run before it is used in other files.
This error could be happening because your mongoose models aren't executed before they are used in some other file.
did you try using "Progressions" while passing it in populate ?