I am trying to convert data into formdata. As you can see here upto input of onSubmit
, the data is showing fine, But, I tried it to append as formData, but its not working.
const onSubmit = async (input) => {
console.log("input >>", input);
const formData = formHelper.getFormData(input);
console.log("formData >>", formData);
// submitAction(formData, setError);
};
formHelper.js
const getFormData = (object) =>
Object.keys(object).reduce((formData, key) => {
if (object[key] instanceof File) {
formData.append(key, object[key][0]);
} else {
formData.append(key, object[key]);
}
return formData;
}, new FormData());
const formHelper = {
getFormData,
};
export default formHelper;
When logging a formData
object with just console.log(formData)
it always returns empty, as you can't log formData
. If you just have to log it before sending it, you can use entries()
to get the entries in the formData
object, like this:
for (var key of formData.entries()) {
console.log(key[0] + ", " + key[1]);
}