const formData = new FormData();
const fields = {
title: bookingTitle,
image: image,
phone_number: phone || null
};
Object.entries(fields).forEach(([key, value]) => {
formData.append(key, value);
});
return formData;
}
I have a function that creates a FormData object, which handles both string and file inputs. One of the fields, phone_number, is optional, so I want to pass null when the user doesn’t provide a value.
However, when I append null to the FormData object, it converts it to the string “null” instead of keeping it as a null value.
Since FormData is designed to handle strings and files, is there a way I can correctly represent phone_number as null or exclude it entirely when it’s not provided?