Skip to content

Commit 44dd2dd

Browse files
committed
fix: handle getter for single nested that returns non-object #42
1 parent f945539 commit 44dd2dd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function applyGettersToDoc(schema, doc) {
117117
val[i] = schematype.caster.applyGetters(val[i], doc);
118118
}
119119
}
120-
} if (val && schematype.$isSingleNested) {
120+
} if (val && typeof val === 'object' && schematype.$isSingleNested) {
121121
applyGettersToDoc.call(this, schematype.schema, val);
122122
} else {
123123
mpath.set(path, val, doc);

test/index.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,28 @@ describe('mongoose-lean-getters', function() {
526526
})
527527
);
528528
});
529+
530+
it('handles single nested getter that returns primitive (gh-42)', async function() {
531+
const AccountSchema = new mongoose.Schema({
532+
name: {
533+
_id: false,
534+
type: {
535+
first: { type: String, required: true },
536+
last: { type: String, required: true },
537+
},
538+
get: function(v) {
539+
return v.first + v.last;
540+
},
541+
},
542+
});
543+
AccountSchema.plugin(mongooseLeanGetters);
544+
const Account = mongoose.model('gh-42-subdoc-getter', AccountSchema);
545+
const { _id } = await Account.create({
546+
name: { first: 'Hamo', last: 'Boker' },
547+
});
548+
const account = await Account.findById(_id).lean({
549+
getters: true,
550+
});
551+
assert.strictEqual(account.name, 'HamoBoker');
552+
});
529553
});

0 commit comments

Comments
 (0)