From 803090d3e31d780b7b9533dda85791143929e1ba Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 7 Jan 2020 21:40:55 -0500 Subject: [PATCH] fix(schema): make aliases handle mongoose-lean-virtuals Backport fix for #6069 and vkarpov15/mongoose-lean-virtuals#6 --- lib/schema.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index 5ed761ad677..ca4dc3c2d6a 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -143,16 +143,20 @@ function aliasFields(schema) { if (alias) { if ('string' === typeof alias && alias.length > 0) { - if (schema.aliases[alias]) + if (schema.aliases[alias]) { throw new Error('Duplicate alias, alias ' + alias + ' is used more than once'); - else + } else { schema.aliases[alias] = prop; + } schema .virtual(alias) .get((function(p) { return function() { - return this.get(p); + if (typeof this.get === 'function') { + return this.get(p); + } + return this[p]; }; })(prop)) .set((function(p) {