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

Returning null when fields is an empty array or undefined #40

Closed
vitorgamer58 opened this issue Oct 17, 2023 · 0 comments · Fixed by vitorgamer58/herbs2mongo#1 · May be fixed by #41
Closed

Returning null when fields is an empty array or undefined #40

vitorgamer58 opened this issue Oct 17, 2023 · 0 comments · Fixed by vitorgamer58/herbs2mongo#1 · May be fixed by #41
Labels
bug Something isn't working

Comments

@vitorgamer58
Copy link
Contributor

vitorgamer58 commented Oct 17, 2023

Describe the bug
When an array is empty [] in the database, it is being converted to null when passed to the entity.
This bug was introduced in the commit 2453cb7
It has already been described in this comment (#37 (comment)) the reason why checker.isEmpty could not be used in this line. At least not returning null.

The reason why this bug ended up being implemented was due to an unhandled exception when the field was declared as an array but was undefined in the database, and when returning value.map((i) => parser(i)) ends generating an exception, as described in this issue: Exception value.map is not a function when array field is undefined in collection

Therefore, there is a need to use either checker.isEmpty or if (value == undefined || value == null).

The checker would be the most appropriate, because, being undefined, null or empty array, the value itself is returned without carrying out any additional processing.

The correction is very important because it is a breaking change implemented in version 4.0.1 of Herbs2Mongo.

To Reproduce
Save an object to the database whose entity has an array field, with that field being an empty array.
When searching for this object, instead of an empty array, it will return null.

Expected behavior
As a complement to the issue #39, it should return the value that is in the database if it is null, undefined if the key does not exist in the database, and empty array if it is like this in the database.
The MongoDB library and database handle whether a value will be null or undefined during a database search. The necessary adjustment is in how this value is converted to an entity.

@vitorgamer58 vitorgamer58 added the bug Something isn't working label Oct 17, 2023
vitorgamer58 added a commit to vitorgamer58/herbs2mongo that referenced this issue Oct 17, 2023
Fix the return of null when the field is an undefined or empty array in the collection

herbsjs#40
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
1 participant