Skip to content

Commit b1b7265

Browse files
authored
Merge pull request #36 from Sebmaster/fix/nulled-array
Fix errors on null value array types
2 parents 32e828a + 2553f51 commit b1b7265

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,17 @@ function applyGettersToDoc(schema, doc, fields, prefix) {
124124

125125
const pathExists = mpath.has(path, doc);
126126
if (pathExists) {
127-
if (schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
127+
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
128+
if (val && schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
128129
mpath.set(
129130
path,
130-
schematype.applyGetters(mpath.get(path, doc), doc, true).map(subdoc => {
131+
val.map(subdoc => {
131132
return schematype.caster.applyGetters(subdoc, doc);
132133
}),
133134
doc
134135
);
135136
} else {
136-
mpath.set(path, schematype.applyGetters(mpath.get(path, doc), doc, true), doc);
137+
mpath.set(path, val, doc);
137138
}
138139
}
139140
});

test/index.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ describe('mongoose-lean-getters', function() {
4545
assert.equal(doc.arr[0].test, 'baz');
4646
});
4747

48+
it('with nulled array', async function() {
49+
const schema = mongoose.Schema({
50+
arr: [String]
51+
});
52+
schema.plugin(mongooseLeanGetters);
53+
54+
const Model = mongoose.model('withNulledArray', schema);
55+
56+
await Model.deleteMany({});
57+
await Model.create({ arr: null });
58+
59+
const doc = await Model.findOne().lean({ getters: true });
60+
61+
assert.equal(doc.arr, null);
62+
});
63+
4864
it('only calls getters once with find() (gh-1)', async function() {
4965
const schema = mongoose.Schema({
5066
name: {

0 commit comments

Comments
 (0)