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
Hydrate does not call setters on the schema #11653
Comments
This is currently expected behavior. We can add an option to |
@vkarpov15 thanks for the reply! We're currently caching an array of mongoose objects, however, the storage mechanism only allows for plain old JavaScript objects. So we want to hydrate the POJOs when we need to use it and ensure that the mongoose object isn't treated as a "new" object hence we don't use the |
Where do the POJOs in your cache come from? |
Hi @vkarpov15, the POJOs are from mongoose documents, the result of |
@zxlin so those documents should have setters run on them at some point, at least when they're created. One alternative approach is to call |
Hi @vkarpov15, so the flow is something like the below, and let's assume we have a field called text: {
type: String,
set: (v) => v.toLowerCase(),
get : (v) => v.toUpperCase(),
} So the steps:
This is the simplified example, hopefully this helps you better understand where we're encountering this issue. |
Bug report
Steps to Reproduce
Example closely modeled from docs on setters: https://mongoosejs.com/docs/tutorials/getters-setters.html#setters
Expected Behavior
Basically the below 2 functions should yield the same thing, but it currently does not
Versions
mongoose@6.2.10
Node v14.19.1
MongoDB: 4.4.6
Possible Resolutions
I understand why setters might not be called, if it was truly hydrating, then perhaps the setter is redundant, but in the case where a schema field may have a setter and getter, this does become necessary. So option 1 is to call the setter always, option 2 is a parameter option to enable calling the setter. I'm not sure what is the best approach, wanted to get some feedback here.
Thanks for the great work everyone!
The text was updated successfully, but these errors were encountered: