mongoose-lean-virtuals icon indicating copy to clipboard operation
mongoose-lean-virtuals copied to clipboard

Bug : parent() does not work to get up in the object more than once

Open qraynaud opened this issue 2 years ago • 0 comments

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.

qraynaud avatar Feb 16 '23 08:02 qraynaud