mongoose-lean-virtuals
mongoose-lean-virtuals copied to clipboard
Bug : parent() does not work to get up in the object more than once
I have a very nested structure and need have the following getter for one of my object:
const getParent = (doc: Document | any): any => {
return doc instanceof Document
? (doc as Types.Subdocument).parent()
: mongooseLeanVirtuals.parent(doc);
};
MySchema.virtual("attr").get(function (this: BaseValuesType): number | undefined {
const parent = getParent(this);
const parentOfParent = getParent(parent);
// do something with parentOfParent
};
This works without a problem doing .toObject()
on a constructed instance but it fails with parentOfParent
being undefined
when using lean({ virtuals: true })
on a query.