Skip to content

Commit

Permalink
Merge pull request #36 from Sebmaster/fix/nulled-array
Browse files Browse the repository at this point in the history
Fix errors on null value array types
  • Loading branch information
vkarpov15 authored Aug 4, 2024
2 parents 32e828a + 2553f51 commit b1b7265
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,17 @@ function applyGettersToDoc(schema, doc, fields, prefix) {

const pathExists = mpath.has(path, doc);
if (pathExists) {
if (schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
if (val && schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
mpath.set(
path,
schematype.applyGetters(mpath.get(path, doc), doc, true).map(subdoc => {
val.map(subdoc => {
return schematype.caster.applyGetters(subdoc, doc);
}),
doc
);
} else {
mpath.set(path, schematype.applyGetters(mpath.get(path, doc), doc, true), doc);
mpath.set(path, val, doc);
}
}
});
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe('mongoose-lean-getters', function() {
assert.equal(doc.arr[0].test, 'baz');
});

it('with nulled array', async function() {
const schema = mongoose.Schema({
arr: [String]
});
schema.plugin(mongooseLeanGetters);

const Model = mongoose.model('withNulledArray', schema);

await Model.deleteMany({});
await Model.create({ arr: null });

const doc = await Model.findOne().lean({ getters: true });

assert.equal(doc.arr, null);
});

it('only calls getters once with find() (gh-1)', async function() {
const schema = mongoose.Schema({
name: {
Expand Down

0 comments on commit b1b7265

Please sign in to comment.