Skip to content

Commit

Permalink
Merge pull request #13793 from Automattic/vkarpov15/gh-13774
Browse files Browse the repository at this point in the history
fix(document): avoid double-calling array getters when using `.get('arr.0')`
  • Loading branch information
vkarpov15 committed Aug 29, 2023
2 parents 29de9c4 + 41e63bd commit d9a4bc4
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
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
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 d9a4bc4

Please sign in to comment.