Skip to content

Entity returning keys containing string undefined instead of undefined #36

@vitorgamer58

Description

@vitorgamer58

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions