DEV Community

Discussion on: Designing a better architecture for a Node.js API

Collapse
 
alexn93 profile image
Aleksandar Nikolov

I tried adding another model(Comment) that is related to Post, but when I try to add .populate('comments') to getAll method in Service class I get
MissingSchemaError: Schema hasn't been registered for model "Comment".
This is how I've added it to Post model
comments: [
{
type: Schema.Types.ObjectId,
ref: 'Comment'
}
],

Collapse
 
pacheco profile image
Thiago Pacheco

Hi Aleksandar,

The property ref must receive the name of the schema you added in mongoose.model, not the name of the class you created.
For example, if your Comment schema is exporting the following: mongoose.model("comments"), then your ref should be: 'ref': 'comments'.

Let me know if that makes sense and if it solves the problem :)