Closed arpitbhs closed 5 years ago
Mongoose doesn't support declaring virtuals on child schemas using this syntax, you would need to define the someOtherThing
virtual on childSchema
:
childSchema.virtual('someOtherThing').get(function() {
return 'someOtherThing';
});
As mentioned in my inital post, I have declared something
on the childSchema itself like childSchema.virtual('something').get(function() { return 'something'; });
It still wont appear
Bump! Any help @vkarpov15 ? I was doing exactly the same thing as you suggested (defining the virtual on the child schema). You didn't read the full post.
I'll check this out. FYI I view GitHub notifications oldest first, so 'bump' messages bump you to the back of the queue, not the front.
Got it, thanks!
If I have a child schema nested within the main schema, the virtual defined on the child schema doesnt come.
eg.
Now TestModel.find().lean({virtuals:true}).exec() , gives me the 'main' virtual, also 'someOtherThing' virtual as a key of children (not for each array item). But it doesnt give me the child virtual 'something' on each element of the children array. Like -
_id: 5c8955801b7d3179990b2150, name: 'hello', children: [ { _id: 5c8955801b7d3179990b2152, name: 'child1', }, { _id: 5c8955801b7d3179990b2151, name: 'child2', }, someOtherThing: 'someOtherThing' ], main: 'main',