Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No support for .parent() or .ownerDocument()? #40

Closed
jnardone opened this issue Oct 2, 2020 · 2 comments
Closed

No support for .parent() or .ownerDocument()? #40

jnardone opened this issue Oct 2, 2020 · 2 comments

Comments

@jnardone
Copy link

jnardone commented Oct 2, 2020

We have a virtual on a child schema that needs visibility to the parent document.

ChildSchema.virtual('hasAThing').get(function () {
  const parent = this.parent(); // fails!
  return parent.arrayOfThings.some(
    (thing) => thing.status === 'valid' and thing.items.includes(this._id)
  );
});

ParentSchema = new Schema(
{
   _id: { type: ObjectId, required: true },
   arrayOfThings: [ChildSchema]
});

ParentSchema.plugin(mongooseLeanVirtuals);
ParentSchema.set('toJSON', { virtuals: true });
ParentSchema.set('toObject', { virtuals: true });

I was hoping (expecting?) that the ability to see .parent() (or .ownerDocument()) from Mongoose would work here as documented in other places e.g. https://mongoosejs.com/docs/subdocs.html#subdoc-parents

But these are undefined. Should they be? Is there a way of accessing the parent document as part of leanvirtuals?

Stack trace that happens:

Object. (/myProject/src/my.model.js:101:20)
at VirtualType.applyGetters (/myProject/src/node_modules/mongoose/lib/virtualtype.js:142:25)
at attachVirtualsToDoc (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:148:55)
at attachVirtualsToDoc (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:119:7)
at applyVirtualsToResult (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:78:7)
at model.Query.attachVirtuals (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:71:10)
at applyVirtualsToChildren (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:111:20)
at model.Query.attachVirtuals (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:70:3)
at model.Query. (/myProject/src/node_modules/mongoose-lean-virtuals/index.js:43:22)
at /myProject/src/node_modules/mongoose-lean-virtuals/index.js:11:12

(the error occurs on the line where I attempt to call .parent() or .ownerDocument())

How do I reference the parent document from a child virtual when using this library?

Mongoose ver 5.10.2
Mongoose-lean-virtuals ver 0.6.9

@vkarpov15
Copy link
Member

The lack of .parent() is intentional. With lean, you get a raw POJO rather than a Mongoose document, so naturally it won't have a parent() function.

I think the way to go is for us to add a parent() function to the mongoose-lean-virtuals plugin, like require('mongoose-lean-virtuals').parent(myObj), that allows you to get the parent. What do you think @jnardone ?

@jnardone
Copy link
Author

jnardone commented Oct 4, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants