I am trying to post an object with multiple images as an array how do append the images as an array to the formdata
const onSubmit = async (e) => {
const formData = new FormData();
formData.append("image", values.images);
formData.append("title", values.title);
formData.append("price", values.price);
formData.append("quantity", values.quantity);
formData.append("id", id);
try {
const res = await axios.post(
"//localhost:4000/products/saveProduct",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
/////////////
<form onSubmit={handleSubmit(onSubmit)}>
<div className="custom-file mb-4">
<input
type="file"
className="custom-file-input"
name="image"
multiple
placeholder="choose file"
onChange={handleChange}
/>
//////
const handleChange = (e) => {
const { type, name } = e.target;
const getValue = () => {
if (type === "checkbox") {
return e.target.checked;
} else if (type === "select-multiple") {
return Array.from(e.target.selectedOptions).map(
(option) => option.value
);
} else if (type === "file") {
for (let i = 0; i < e.target.files.length; i++) {
e.target.files[i].url = URL.createObjectURL(e.target.files[i]);
}
return Array.from(e.target.files);
}
return e.target.value;
};
Also When I send this to my back end I am getting to an empty object as the req.body