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

Entity returning keys containing string undefined instead of undefined #36

Open
vitorgamer58 opened this issue Jun 28, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@vitorgamer58
Copy link
Contributor

vitorgamer58 commented Jun 28, 2023

Describe the bug
When calling the find method, the Herbs2Mongo Repository calls a method of the DataMapper class to convert each line returned into an entity type from @herbsjs/gotu, however, when the function returns, it fills in the fields of the entity that were not returned from the database, with a string containing the word undefined, instead of the javascript type undefined.

This can lead to several failures, since in checker.isEmpty the string "undefined" is not considered empty, while the type undefined is.

To Reproduce
Steps to reproduce the behavior:

  1. Create a project that uses Herbs2Mongo, configure a User entity with the fields id, nickname, type and password, being the type field of type String.
  2. Include only id, nickname and password fields.
  3. Call the find function in the user search usecase.
  4. The type field will contain a string containing the word undefined

Expected behavior
It is expected that the fields declared in the entity that are not in the database, or are returned as undefined, or that the key is not even included in the return object.
It is up to the user to verify using .isValid() or checking the fields, as well as treating entities that do not have certain fields according to business rules.

Screenshots

Evidence of returning the array of entities containing "undefined" after calling the toEntity method of the DataMapper class.
Find Method

Location in the DataMapper class where the bug is most likely happening.
toEntity method

Additional context
I found this bug when searching for a list of customers, which had the _id and chat_id fields, but the new version of my product would now have the additional fields: username, group and type.
I would have a business rule to check if type was null or undefined using the herbsjs checker, and then ask those users to update their registration.

As a temporary solution, I changed the check to verify that the resulting string was strictly equal to "undefined"
https://github.com/vitorgamer58/telegram-bot-bovespa-herbs/blob/master/src/domain/usecases/verificaCadastro.js#L60

@vitorgamer58 vitorgamer58 added the bug Something isn't working label Jun 28, 2023
@vitorgamer58
Copy link
Contributor Author

The problem happens because the dataParser function only checks if the field is strictly equal to null, however if the field does not exist in the database, it will be undefined and not null.
Then it passes to the parser an undefined, which returns a string written "undefined"
We can use a comparison if it's null or undefined to return null.
parser function

vitorgamer58 added a commit to vitorgamer58/herbs2mongo that referenced this issue Oct 21, 2023
Using checker.isEmpty null, undefined, {} or "" values will be returned by themselves, without being
converted to null or undefined and without being passed to the parser.

BREAKING CHANGE: This can change the way a value that does not exist in the collection is
undefined in the application that uses the lib.
A null value is returned as null, but an
undefined is passed to the parser and can return a different value.
Instead of "undefined",
undefined will be returned, causing breaking changes in systems that expect "undefined" with a
string type.

herbsjs#36 and herbsjs#39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant