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

[bug] Transform functions are applied to nested documents #8260

Closed
tarantoj opened this issue Oct 19, 2019 · 2 comments
Closed

[bug] Transform functions are applied to nested documents #8260

tarantoj opened this issue Oct 19, 2019 · 2 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Milestone

Comments

@tarantoj
Copy link

Do you want to request a feature or report a bug?
Bug report

What is the current behavior?
Applying a transform to a document using toObject() or toJSON() methods is applied to subdocuments, conflicting with online documentation.

Transforms are applied only to the document and are not applied to sub-documents.

If the current behavior is a bug, please provide the steps to reproduce.

Create a document with a subdocument, call toObject() or toJSON() with a transform function.
Test script

What is the expected behavior?
Transform to only be applied to the root document

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: v12.12.0
Mongoose: 5.7.1
MongoDB: N/A

@tarantoj tarantoj changed the title Transform functions are applied to nested documents [bug] Transform functions are applied to nested documents Oct 22, 2019
@vkarpov15 vkarpov15 added this to the 5.7.8 milestone Oct 25, 2019
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Oct 25, 2019
@vkarpov15
Copy link
Collaborator

I think this behavior is intentional and the docs are misleading. The docs refer to transforms defined in the schema, like schema.options.toObject.transform. But if you pass a transform when calling toObject(), it applies to all documents, because otherwise the transform wouldn't be terribly useful.

Is this behavior causing you an issue?

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Oct 26, 2019
@tarantoj
Copy link
Author

Yeah, caused some problems when trying to use an inline transform function to remove sensitive data on an endpoint depending on the requested item and logged in user.

If recursive application of the transform function is intended behaviour, I'd agree with you that the docs could require some clarification, especially as I'm not the only one to find this one out the hard way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants