Skip to content

Commit 41e63bd

Browse files
committed
fix(document): avoid double-calling array getters when using .get('arr.0')
Re: #13748 Re: #13774
1 parent c1d5b76 commit 41e63bd

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,10 @@ exports.getValue = function(path, obj, map) {
640640
const mapGetterOptions = Object.freeze({ getters: false });
641641

642642
function getValueLookup(obj, part) {
643-
const _from = obj?._doc || obj;
643+
let _from = obj?._doc || obj;
644+
if (_from != null && _from.isMongooseArrayProxy) {
645+
_from = _from.__array;
646+
}
644647
return _from instanceof Map ?
645648
_from.get(part, mapGetterOptions) :
646649
_from[part];

test/document.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12386,6 +12386,10 @@ describe('document', function() {
1238612386
assert.equal(oneUser.hobbies[0], 'swimming');
1238712387
assert.equal(oneUser.hobbies[0], 'swimming');
1238812388
assert.equal(oneUser.hobbies[0], 'swimming');
12389+
12390+
assert.equal(oneUser.get('hobbies.0'), 'swimming');
12391+
assert.equal(oneUser.get('hobbies.0'), 'swimming');
12392+
assert.equal(oneUser.get('hobbies.0'), 'swimming');
1238912393
});
1239012394

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

0 commit comments

Comments
 (0)