Skip to content
Advertisement

Mongoose populate subdocument in array

I have a Mongoose offer model explained below:

const OfferSchema = new Schema({
  sections: [
    {
      title: String,
    },
  ],
});

and order schema which has reference to to the first schema offer explained below:

const OrderSchema = new Schema({
  offers: [
    {
      offer: { type: Schema.Types.ObjectId, ref: 'Offer' },
      sections: [
        {
          section: { type: Schema.Types.ObjectId, ref: 'Offer.sections' }, // issue here
        },
      ],
    },
  ],
});

the problem that I can not populate sections here {section: { type: Schema.Types.ObjectId, ref: 'Offer.sections' }}

it gives me MissingSchemaError: Schema hasn't been registered for model "Offer.sections".

so is there any way to populate sections?

Advertisement

Answer

Unfortunately, Mongoose doesn’t support this feature. check the Github issue here

The alternative solution you can embed sections into the order schema

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement