diff --git a/lib/helpers/populate/getModelsMapForPopulate.js b/lib/helpers/populate/getModelsMapForPopulate.js index abb68b7bf09..990f8a64c32 100644 --- a/lib/helpers/populate/getModelsMapForPopulate.js +++ b/lib/helpers/populate/getModelsMapForPopulate.js @@ -78,12 +78,14 @@ module.exports = function getModelsMapForPopulate(model, docs, options) { } } - const virtual = getVirtual(model.schema, options.path); + const _virtualRes = getVirtual(model.schema, options.path); + const virtual = _virtualRes == null ? null : _virtualRes.virtual; + let localField; let count = false; if (virtual && virtual.options) { - const virtualPrefix = virtual.$nestedSchemaPath ? - virtual.$nestedSchemaPath + '.' : ''; + const virtualPrefix = _virtualRes.nestedSchemaPath ? + _virtualRes.nestedSchemaPath + '.' : ''; if (typeof virtual.options.localField === 'function') { localField = virtualPrefix + virtual.options.localField.call(doc, doc); } else { @@ -312,7 +314,8 @@ module.exports = function getModelsMapForPopulate(model, docs, options) { } else { schemaForCurrentDoc = schema; } - const virtual = getVirtual(modelForCurrentDoc.schema, options.path); + const _virtualRes = getVirtual(modelForCurrentDoc.schema, options.path); + const virtual = _virtualRes == null ? null : _virtualRes.virtual; let ref; let refPath; diff --git a/lib/helpers/populate/getVirtual.js b/lib/helpers/populate/getVirtual.js index dd2644d5150..056828b85ec 100644 --- a/lib/helpers/populate/getVirtual.js +++ b/lib/helpers/populate/getVirtual.js @@ -8,7 +8,7 @@ module.exports = getVirtual; function getVirtual(schema, name) { if (schema.virtuals[name]) { - return schema.virtuals[name]; + return { virtual: schema.virtuals[name], path: void 0 }; } const parts = name.split('.'); let cur = ''; @@ -17,8 +17,7 @@ function getVirtual(schema, name) { cur += (cur.length > 0 ? '.' : '') + parts[i]; if (schema.virtuals[cur]) { if (i === parts.length - 1) { - schema.virtuals[cur].$nestedSchemaPath = nestedSchemaPath; - return schema.virtuals[cur]; + return { virtual: schema.virtuals[cur], path: nestedSchemaPath }; } continue; } @@ -33,20 +32,24 @@ function getVirtual(schema, name) { if (schema.virtuals[rest]) { if (i === parts.length - 2) { - schema.virtuals[rest].$nestedSchemaPath = - [nestedSchemaPath, cur].filter(v => !!v).join('.'); - return schema.virtuals[rest]; + return { + virtual: schema.virtuals[rest], + nestedSchemaPath:[nestedSchemaPath, cur].filter(v => !!v).join('.') + }; } continue; } if (i + 1 < parts.length && schema.discriminators) { for (const key of Object.keys(schema.discriminators)) { - const _virtual = getVirtual(schema.discriminators[key], rest); - if (_virtual != null) { - _virtual.$nestedSchemaPath = [nestedSchemaPath, cur]. + const res = getVirtual(schema.discriminators[key], rest); + if (res != null) { + const _path = [nestedSchemaPath, cur, res.nestedSchemaPath]. filter(v => !!v).join('.'); - return _virtual; + return { + virtual: res.virtual, + nestedSchemaPath: _path + }; } } } diff --git a/test/helpers/populate.getVirtual.test.js b/test/helpers/populate.getVirtual.test.js index 16facb7e8a9..6f6f2d75454 100644 --- a/test/helpers/populate.getVirtual.test.js +++ b/test/helpers/populate.getVirtual.test.js @@ -39,7 +39,7 @@ describe('getVirtual', function() { product: { type: String } })); - assert.equal(getVirtual(batchSchema, 'nested.events.users_$').options.ref, + assert.equal(getVirtual(batchSchema, 'nested.events.users_$').virtual.options.ref, 'Users'); done(); @@ -80,8 +80,8 @@ describe('getVirtual', function() { // Second embedded discriminator docArray.discriminator('Purchased', new Schema({ product: String })); - const virtual = getVirtual(batchSchema, 'nested.events.nestedLayer.users_$'); - assert.equal(virtual.options.ref, 'Users'); + const res = getVirtual(batchSchema, 'nested.events.nestedLayer.users_$'); + assert.equal(res.virtual.options.ref, 'Users'); done(); }); @@ -106,13 +106,13 @@ describe('getVirtual', function() { docArray.discriminator('Clicked', clickedSchema); - let virtual = getVirtual(batchSchema, 'events.users_$'); - assert.equal(virtual.options.ref, 'Users'); - assert.equal(virtual.$nestedSchemaPath, 'events'); + let res = getVirtual(batchSchema, 'events.users_$'); + assert.equal(res.virtual.options.ref, 'Users'); + assert.equal(res.nestedSchemaPath, 'events'); - virtual = getVirtual(batchSchema, 'events.users_$'); - assert.equal(virtual.options.ref, 'Users'); - assert.equal(virtual.$nestedSchemaPath, 'events'); + res = getVirtual(batchSchema, 'events.users_$'); + assert.equal(res.virtual.options.ref, 'Users'); + assert.equal(res.nestedSchemaPath, 'events'); done(); }); diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 8256c2600c6..51c95ca766c 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -8628,7 +8628,7 @@ describe('model: populate:', function() { }); }); - it('handles virtual populate underneath embedded discriminator nested path (gh-6488) (gh-8173)', function() { + it('handles virtual populate of an embedded discriminator nested path (gh-6488) (gh-8173)', function() { return co(function*() { const UserModel = db.model('gh6488_User', Schema({ employeeId: Number, @@ -8646,10 +8646,8 @@ describe('model: populate:', function() { }); const docArray = batchSchema.path('nested.events'); - const Clicked = docArray. - discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema })); - const Purchased = docArray. - discriminator('gh6488_Purchased', Schema({ purchased: String })); + docArray.discriminator('gh6488_Clicked', Schema({ nestedLayer: nestedLayerSchema })); + docArray.discriminator('gh6488_Purchased', Schema({ purchased: String })); const Batch = db.model('gh6488', batchSchema);