0

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"
    }]
});

3 Answers 3

1

Refer to your schema, look like you just need to do:

Merchant.find({cloverId: merchantId}).populate("ingredients.ingredient")...
Sign up to request clarification or add additional context in comments.

4 Comments

When I do this it is just giving me the actual id, like this:
inventory: [ { _id: 5d97508e7929f31fb64a3196, quantity: 123 }, ...
What is inventory? I don't see it in your schema.
Inventory is the ejs page being rendered. However, I figured out my problem. Refactored and messed some things up in the database. Got it straightened out and this way of doing populate works perfectly. Thank you.
0

I will answer my own question in case others have a similar problem. My problem was that I renamed some variables but forgot to do it when saving data to the database. Stupid mistake, but just a reminder to be careful when refactoring. I used Coung Le Ngoc's answer, it works perfectly.

Comments

0

You can try the below code.

Merchant.find({otherId: merchantId})
  .populate({ path: 'ingredients' })
  .exec(function(err, docs) {

    var options = {
      path: 'ingredients.components',
      model: 'Ingredient'
    };

    if (err) return res.json(500);
    Merchant.populate(docs, options, function (err, merchant) {
      res.json(merchant);
    });
  });

1 Comment

When I add this I just get an error about Project not being defined. I don't really understand what this code is doing so I am not sure how to edit it for my needs...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.