Skip to content

Fix: prevent null/undefined in Snowboard.request createFormData #1386

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

phpiando
Copy link

Problem

When using Snowboard.request() with data that includes null or undefined values, a TypeError is thrown due to Object.entries() not accepting such values.

Fix

A simple check for null or undefined was added at the beginning of the createFormData() function to prevent the error and silently skip those fields.

Reproduction Example

Snowboard.request('onSomething', {
    data: {
        name: 'phpiando',
        optionalField: null // This causes an error in current version
    }
});

This PR prevents that error.

@LukeTowers
Copy link
Member

@phpiando so with this code change your example code would actually just send a request with name defined in the form data and not provide anything for optionalField at all? I feel like that's not necessarily what I would expect to happen. @bennothommo any thoughts on this one?

@LukeTowers LukeTowers requested a review from bennothommo July 16, 2025 20:12
@LukeTowers LukeTowers added needs response Issues/PRs where a maintainer is awaiting a response from the submitter needs review Issues/PRs that require a review from a maintainer labels Jul 16, 2025
@phpiando
Copy link
Author

@phpiando so with this code change your example code would actually just send a request with name defined in the form data and not provide anything for optionalField at all? I feel like that's not necessarily what I would expect to happen. @bennothommo any thoughts on this one?

Yep! this change would result in fields with null or undefined values being skipped entirely in the FormData.
In our use case, that behavior is preferred to avoid errors like:

TypeError: Cannot convert undefined or null to object

But!
Maybe a better solution would be to treat null and undefined as empty strings when appending to FormData, like:

if (data === null || data === undefined) {
    formData.append(prefix, '');
    return;
}

That way, the key is still sent, but it doesn't cause errors.

@LukeTowers
Copy link
Member

LukeTowers commented Jul 17, 2025

That could work, but I'll wait for @bennothommo's thoughts on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs response Issues/PRs where a maintainer is awaiting a response from the submitter needs review Issues/PRs that require a review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants