Skip to content

Commit

Permalink
fix(populate): allow accessing populate virtual prop underneath array…
Browse files Browse the repository at this point in the history
… when virtual defined on top level

Fix #8198
Re: #8210
  • Loading branch information
vkarpov15 committed Oct 3, 2019
1 parent f8db7ce commit dc0025b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/schema.js
Expand Up @@ -10,6 +10,7 @@ const SchemaType = require('./schematype');
const VirtualType = require('./virtualtype');
const applyTimestampsToChildren = require('./helpers/update/applyTimestampsToChildren');
const applyTimestampsToUpdate = require('./helpers/update/applyTimestampsToUpdate');
const arrayParentSymbol = require('./helpers/symbols').arrayParentSymbol;
const get = require('./helpers/get');
const getIndexes = require('./helpers/schema/getIndexes');
const handleTimestampOption = require('./helpers/schema/handleTimestampOption');
Expand Down Expand Up @@ -1724,6 +1725,22 @@ Schema.prototype.virtual = function(name, options) {
return mem[part];
}, this.tree);

// Workaround for gh-8198: if virtual is under document array, make a fake
// virtual. See gh-8210
let cur = parts[0];
for (let i = 0; i < parts.length - 1; ++i) {
if (this.paths[cur] != null && this.paths[cur].$isMongooseDocumentArray) {
const remnant = parts.slice(i + 1).join('.');
const v = this.paths[cur].schema.virtual(remnant);
v.get((v, virtual, doc) => {
const parent = doc.__parentArray[arrayParentSymbol];
const path = cur + '.' + doc.__index + '.' + remnant;
return parent.get(path);
});
break;
}
}

return virtuals[name];
};

Expand Down

0 comments on commit dc0025b

Please sign in to comment.