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

Getter firing every time on array property #13748

Closed
2 tasks done
dantenol opened this issue Aug 17, 2023 · 3 comments
Closed
2 tasks done

Getter firing every time on array property #13748

dantenol opened this issue Aug 17, 2023 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@dantenol
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.4.3

Node.js version

18.17

MongoDB server version

6.0.6

Typescript version (if applicable)

5.1.6

Description

While using getters on a schema with type array, the getter function is called every time the value is accessed. The expected behavior should be run the function only once.

Given this getter function on user.hobbies[]

const removeSufix = (name: string) => {
  return name.slice(0, -6);
};

this is the current result of two console.logs in sequence

  console.log(user.hobbies); // ["swimming", "football"] => correct value
  console.log(user.hobbies); // ["sw", "fo"] => runs getter again

Steps to Reproduce

https://github.com/dantenol/mongoose-array-getters-demo

Expected Behavior

Run the getter function only once.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Aug 17, 2023

Well that it gets called everytime makes sense. That it slices everytime not, because strings as all primitives are immutable.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Aug 17, 2023

Can you call console.log(typeof name) in the getter?
Maybe return ('' + name).slice(0, -6)

@dantenol
Copy link
Author

type is always string

therefore, changing to the suggested code to force a string had the same results

@vkarpov15 vkarpov15 added this to the 7.4.4 milestone Aug 21, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 21, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.4.4, 7.4.5 Aug 22, 2023
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 23, 2023
vkarpov15 added a commit that referenced this issue Aug 23, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.4.5, 7.5 Aug 25, 2023
vkarpov15 added a commit that referenced this issue Aug 28, 2023
fix(document): make array getters avoid unintentionally modifying array, defer getters until index access instead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants