Skip to content

Commit 3f7fed0

Browse files
committed
fix: apply single nested subdoc getter results
1 parent 9042902 commit 3f7fed0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ function applyGettersToDoc(schema, doc) {
117117
val[i] = schematype.caster.applyGetters(val[i], doc);
118118
}
119119
}
120-
} if (val && typeof val === 'object' && schematype.$isSingleNested) {
120+
} else if (val && typeof val === 'object' && schematype.$isSingleNested) {
121121
applyGettersToDoc.call(this, schematype.schema, val);
122-
} else {
123-
mpath.set(path, val, doc);
124122
}
123+
124+
mpath.set(path, val, doc);
125125
});
126126
}
127127

test/index.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,31 @@ describe('mongoose-lean-getters', function() {
550550
});
551551
assert.strictEqual(account.name, 'HamoBoker');
552552
});
553+
554+
it('applies single nested subdoc getters (gh-45)', async function() {
555+
const priceSchema = new mongoose.Schema(
556+
{
557+
amount: mongoose.Schema.Types.Decimal128,
558+
currency: String,
559+
},
560+
{ _id: false }
561+
);
562+
563+
const itemSchema = new mongoose.Schema({
564+
name: String,
565+
price: {
566+
type: priceSchema,
567+
get: v => ({ value: parseFloat(v.amount.toString()), unit: v.currency })
568+
},
569+
});
570+
itemSchema.plugin(mongooseLeanGetters);
571+
const Item = mongoose.model('gh-45-single-nested-getters', itemSchema);
572+
await Item.create({
573+
name: 'Test Product',
574+
price: { amount: 123.45, currency: 'USD' },
575+
});
576+
577+
const lean = await Item.findOne().lean({ getters: true });
578+
assert.deepStrictEqual(lean.price, { value: 123.45, unit: 'USD' });
579+
});
553580
});

0 commit comments

Comments
 (0)