Mongoose Populate cant get this to work? Mongoose Populate cant get this to work? mongoose mongoose

Mongoose Populate cant get this to work?


You are right to use the populate for loading the subdocument, but you need to pass the name of the book field in the novel schema, in your case the name is type.

Here is the link to documentation with some examples: Mongoose Populate

And below is the more one solution to your problem:

let book = await new BookModel({name:"book test"}).save();console.log('-----------BOOK ITEM-------------');console.log(book);let novel = await new NovelModel({name:"novel test",type:book._id}).save();console.log('-----------NOVEL ITEM-------------');console.log(novel);let itemPopulated = await NovelModel.findById(novel._id)    .populate('type')    .then((result) => {        return result;    }).catch((err) => {        console.log(err);});console.log('-----------ITEM POPULATED-------------');console.log(itemPopulated);

And the execution output:

execution output


The parameter on the function populate() is the path of the field you want to populate, and also when you use populate or any chained function in mongoose you have to use the exec() function in the ending, so the correct way to do it would be:

Novel.findById('5b87310d41073743856a7c4a').populate({  path: 'type'}).exec()