Skip to content

Commit

Permalink
fix(populate): make retainNullValues set array element to null if…
Browse files Browse the repository at this point in the history
… foreign doc with that id was not found

Fix #8293
  • Loading branch information
vkarpov15 committed Nov 6, 2019
1 parent f0aeddc commit 9b0e96d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/helpers/populate/assignRawDocsToIdStructure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const modelSymbol = require('../symbols').modelSymbol;

module.exports = assignRawDocsToIdStructure;

/*!
Expand Down Expand Up @@ -63,8 +65,10 @@ function assignRawDocsToIdStructure(rawIds, resultDocs, resultOrder, options, re
} else {
newOrder.push(doc);
}
} else if (id != null && id[modelSymbol] != null) {
newOrder.push(id);
} else {
newOrder.push(nullIfNotFound ? null : id);
newOrder.push(options.retainNullValues || nullIfNotFound ? null : id);
}
} else {
// apply findOne behavior - if document in results, assign, else assign null
Expand Down
3 changes: 3 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8795,6 +8795,9 @@ describe('model: populate:', function() {
});
assert.equal(doc.troops.length, 4);
assert.equal(doc.troops[0], null);
assert.equal(doc.troops[1].name, 'Card 2');
assert.equal(doc.troops[2].name, 'Card 3');
assert.equal(doc.troops[3].name, 'Card 4');
});
});
});
Expand Down

0 comments on commit 9b0e96d

Please sign in to comment.