Skip to content

Commit

Permalink
Merge pull request #13842 from Automattic/vkarpov15/gh-13839
Browse files Browse the repository at this point in the history
Edits to Handle null objects in arrays during populate operations
  • Loading branch information
vkarpov15 authored Sep 8, 2023
2 parents 41de90c + e42aaaf commit f552fe2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/helpers/populate/markArraySubdocsPopulated.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module.exports = function markArraySubdocsPopulated(doc, populated) {

if (utils.isMongooseDocumentArray(val)) {
for (let j = 0; j < val.length; ++j) {
val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item);
if (val[j]) {
val[j].populated(rest, item._docs[id] == null ? void 0 : item._docs[id][j], item);
}
}
break;
}
Expand Down
22 changes: 22 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10285,6 +10285,28 @@ describe('model: populate:', function() {

});

it('handles populating underneath document arrays that have null (gh-13839)', async function() {
const schema = new Schema({
children: [
{
doc: { type: mongoose.Schema.Types.ObjectId, ref: 'Child' },
name: String
}
]
});
const Parent = db.model('Parent', schema);
const Child = db.model('Child', new Schema({ name: String }));

const child = new Child({ name: 'gh-13839-test' });
await child.save();
let parent = new Parent({ children: [{ doc: child._id, name: 'foo' }, null] });
await parent.save();

parent = await Parent.findById(parent._id).populate('children.doc');
assert.equal(parent.children.length, 2);
assert.equal(parent.children[0].doc.name, 'gh-13839-test');
assert.equal(parent.children[1], null);
});

describe('strictPopulate', function() {
it('reports full path when throwing `strictPopulate` error with deep populate (gh-10923)', async function() {
Expand Down

0 comments on commit f552fe2

Please sign in to comment.