Samir Taher
Samir Taher

Reputation: 25

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?

Upvotes: 0

Views: 55

Answers (1)

tarek noaman
tarek noaman

Reputation: 1503

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

The alternative solution you can embed sections into the order schema

Upvotes: 1

Related Questions