Skip to content

Commit

Permalink
fix(document): avoid double-calling array getters when using `.get('a…
Browse files Browse the repository at this point in the history
…rr.0')`

Re: #13748
Re: #13774
  • Loading branch information
vkarpov15 committed Aug 28, 2023
1 parent c1d5b76 commit 41e63bd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,10 @@ exports.getValue = function(path, obj, map) {
const mapGetterOptions = Object.freeze({ getters: false });

function getValueLookup(obj, part) {
const _from = obj?._doc || obj;
let _from = obj?._doc || obj;
if (_from != null && _from.isMongooseArrayProxy) {
_from = _from.__array;
}
return _from instanceof Map ?
_from.get(part, mapGetterOptions) :
_from[part];
Expand Down
4 changes: 4 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12386,6 +12386,10 @@ describe('document', function() {
assert.equal(oneUser.hobbies[0], 'swimming');
assert.equal(oneUser.hobbies[0], 'swimming');
assert.equal(oneUser.hobbies[0], 'swimming');

assert.equal(oneUser.get('hobbies.0'), 'swimming');
assert.equal(oneUser.get('hobbies.0'), 'swimming');
assert.equal(oneUser.get('hobbies.0'), 'swimming');
});

it('sets defaults on subdocs with subdoc projection (gh-13720)', async function() {
Expand Down

0 comments on commit 41e63bd

Please sign in to comment.