Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): avoid double-calling array getters when using .get('arr.0') #13793

Merged
merged 1 commit into from Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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