I know that this has been asked a million times, but it doesn't seem to be working for me. I cannot find a way to populate the references in an object when getting it from the database. No matter what I try it either returns an empty list or just the list of id's. What am I doing wrong here?
displayInventory: (req, res)=>{
Merchant.find({otherId: merchantId})
.populate({
path: "ingredients",
populate: {
path: "ingredient",
model: "Ingredient"
}
})
.then((merchant)=>{
console.log(merchant);
if(merchant){
return res.render("./inventory/inventory", {merchant: merchant});
}else{
return res.redirect("/merchant/new");
}
})
.catch((err)=>{
console.log(err);
return res.render("error");
});
}
const MerchantSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
otherId: {
type: String,
required: true
},
lastUpdatedTime: {
type: Date,
default: Date.now
},
ingredients: [{
ingredient: {
type: mongoose.Schema.Types.ObjectId,
ref: "Ingredient"
},
quantity: {
type: Number,
min: [0, "Quantity cannot be less than 0"]
}
}],
recipes: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Recipe"
}]
});