0

Here's my Mongoose schema:

const productSchema = mongoose.Schema({
  writer: {
        type: Schema.Types.ObjectId,
        ref: 'User'
  },
  schedule: [{
            dateTime: {
                type: String,
                unique: 1            
            },
            candidates: [{
                type: Schema.Types.ObjectId,
                ref: 'User'
            }],
            attorn: {
                type: Schema.Types.ObjectId,
                ref: 'User'
            }
        }]
   ...

This is where I get my items along with the candidates:

    Product.find({ '_id': { $in: productIds } })
    .populate('writer')
    .populate({ 
        path: 'schedule',
        populate: {
          path: 'candidates',
          model: 'User'
        } 
     })
    .exec((err, product) => {
        if (err) return res.status(400).send(err)
        return res.status(200).send(product)
    })
  1. Result of this code is a product with only populated 'writer' and empty array for 'candidates'
  2. If I remove the line for populating 'candidates', it returns array of _id for 'candidates'
  3. How do I populate the 'candidates' array?

1 Answer 1

1

can you try this

Product.find({ _id: { $in: productIds } })
  .populate("writer")
  .populate({
    path: "schedule.candidates",
  })
  .populate({
    path: "schedule.attorn",
  })
  .exec((err, product) => {
    if (err) return res.status(400).send(err);
    return res.status(200).send(product);
  });

Sign up to request clarification or add additional context in comments.

Comments

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.