Lupus Lupus - 14 days ago 5
Node.js Question

circular populate throws irrelevant error

I'm getting this error but there is no virtual in the schema.

Error: If you are populating a virtual, you must set the localField and foreignField options


Schema

let kategoriSchemaObject = {
//...
kategoriChildren: [{
type: Schema.Types.ObjectId,
ref: 'Kategori'
}],
//...
}

let kategoriSchema = new Schema(kategoriSchemaObject , {
timestamps: {
createdAt: 'created_at'
}
});

mongoose.model('Kotegori', kategoriSchema );


Query

Categories.find({
kategoriUst: null,
})
.populate({
path: 'kategoriChildren',
options: {
limit: 8,
},
populate: {
path: ' kategoriChildren',
select: 'name',
options: {
limit: 6,
},
},
})
.exec()
.then((data) => {

//...
next();
})
.catch((err) => next(err));


If I remove second populate It works as expected.

Am I missing something here? I also opened an issue about this.

Answer

So as said in comments section:

There were a space in populate(...) description ' kategoriChildren'.

And the category schema got a misspelled name:

You did created your schema as Kotegori and your links as Kategori