Skip to content

Commit 1845a57

Browse files
committed
fix(document): handle nested paths underneath subdocuments when getting all subdocuments for pre save hooks
Fix #11917
1 parent 6d91d94 commit 1845a57

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/document.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3388,7 +3388,7 @@ Document.prototype.$getAllSubdocs = function() {
33883388
}, seed);
33893389
} else if (val && !Array.isArray(val) && val.$isSingleNested) {
33903390
seed = Object.keys(val._doc).reduce(function(seed, path) {
3391-
return docReducer(val._doc, seed, path);
3391+
return docReducer(val, seed, path);
33923392
}, seed);
33933393
seed.push(val);
33943394
} else if (val && utils.isMongooseDocumentArray(val)) {

test/document.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -11473,4 +11473,38 @@ describe('document', function() {
1147311473
baz2.bar = bar2;
1147411474
assert.ok(baz.populated('bar'));
1147511475
});
11476+
11477+
it('$getAllSubdocs gets document arrays underneath a nested path (gh-11917)', function() {
11478+
const nestedSettingsSchema = new Schema({
11479+
value: String,
11480+
active: Boolean
11481+
});
11482+
11483+
const userSettingsSchema = new Schema({
11484+
nestedSettings: {
11485+
settingsProps: [nestedSettingsSchema]
11486+
}
11487+
});
11488+
11489+
const userSchema = new Schema({
11490+
first_name: String,
11491+
last_name: String,
11492+
settings: userSettingsSchema
11493+
});
11494+
11495+
const User = db.model('User', userSchema);
11496+
11497+
const doc = new User({
11498+
settings: {
11499+
nestedSettings: {
11500+
settingsProps: [{ value: 'test', active: true }]
11501+
}
11502+
}
11503+
});
11504+
11505+
const subdocs = doc.$getAllSubdocs();
11506+
assert.equal(subdocs.length, 2);
11507+
assert.equal(subdocs[0].value, 'test');
11508+
assert.ok(subdocs[1].nestedSettings);
11509+
});
1147611510
});

0 commit comments

Comments
 (0)