File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -117,7 +117,7 @@ function applyGettersToDoc(schema, doc) {
117
117
val [ i ] = schematype . caster . applyGetters ( val [ i ] , doc ) ;
118
118
}
119
119
}
120
- } if ( val && schematype . $isSingleNested ) {
120
+ } if ( val && typeof val === 'object' && schematype . $isSingleNested ) {
121
121
applyGettersToDoc . call ( this , schematype . schema , val ) ;
122
122
} else {
123
123
mpath . set ( path , val , doc ) ;
Original file line number Diff line number Diff line change @@ -526,4 +526,28 @@ describe('mongoose-lean-getters', function() {
526
526
} )
527
527
) ;
528
528
} ) ;
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
+ } ) ;
529
553
} ) ;
You can’t perform that action at this time.
0 commit comments