-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Custom toJSON and toObject Implementation Produces Object Instead of Plain Value #15084
Comments
The issue here is that you need to modify the
|
This is something that we will need to add a new feature for. Mongoose does not currently support customizing toObject() and toJSON() output on a per SchemaType basis, but we should be able to support the following:
That would also be helpful for other data types, like setting custom date formats or converting Decimal128 to number in JSON serialization. |
With #15163, you'll be able to do |
Prerequisites
Mongoose version
7.6.3
Node.js version
22.10
MongoDB version
5
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
I'm encountering an issue with implementing custom
toJSON
andtoObject
behavior for a custom type. My goal is to produce different outputs when converting the custom type's value—specifically, I want to output a plain number or string instead of the object representation.However, despite my efforts, the result always includes the object representation, such as:
value: CustomType { value: 1 },
.Below is a minimal reproduction of the issue:
I would greatly appreciate any guidance on resolving this issue or understanding why the current implementation doesn't work as expected.
The text was updated successfully, but these errors were encountered: