You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to create a dynamic ref for an object ID stored inside a property of a subdocument. The property can reference multiple models (even models registered in a different mongoose database instance) so I use the model instance directly instead of a model name.
As you can see, the ref property is the Object ID of either a document from CategoryAModel or CategoryBModel. I started creating documents for this model, such as the following:
constdata={name: 'Test Data',children: [{category: 'catA',ref: '658e9f1901f3da2c14920401'// <- existing document from CategoryAModel},{category: 'catB',ref: '654995e0c89d1c19c84e77b7'// <- existing document from CategoryBModel}]}
But when i tried to populate this, the ref for category: 'catA' becomes null (despite existing). I logged the this context in the ref's ref function and saw that this refers to the document being processed (same shape as the data above), and that this.category is undefined because it's actually inside the children array. Essentially making the ref always result in being the CategoryBModel
Since it's an array, how would I go and make a dynamic reference? Is there a way to access the index of the subSchema being referred to?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to create a dynamic ref for an object ID stored inside a property of a subdocument. The property can reference multiple models (even models registered in a different mongoose database instance) so I use the model instance directly instead of a model name.
As you can see, the
ref
property is the Object ID of either a document fromCategoryAModel
orCategoryBModel
. I started creating documents for this model, such as the following:But when i tried to
populate
this, theref
forcategory: 'catA'
becomes null (despite existing). I logged thethis
context in theref
's ref function and saw thatthis
refers to the document being processed (same shape as the data above), and thatthis.category
is undefined because it's actually inside thechildren
array. Essentially making theref
always result in being theCategoryBModel
Since it's an array, how would I go and make a dynamic reference? Is there a way to access the index of the
subSchema
being referred to?Beta Was this translation helpful? Give feedback.
All reactions