The pattern for using .populate()
:
router.get('/list', (req, res, next) => {
Model
// prettier-ignore
.find()
.populate('property1')
.then(/* everything went OK */)
.catch(/* error */);
});
If we need to perform double populate (populate one property and then in that property again populate some other property), the pattern is:
router.get('/posts', (req, res, next) => {
Model1.find()
.populate('property1 property2')
.populate({
path: 'property2',
populate: {
path: 'property3', // what are we populating inside "property2"
model: 'Model2' // the property we are populating is actually which model
}
})
.then(/* everything went OK */)
.catch(/* error */);
});