Hey there
I have a collection named gig
, and I’m trying to upload a few attachments
and banners
right after creating a Gig.
This is how my POST request looks like
const Formik = useFormik({
initialValues: {
title: "",
category: "",
subCategory: "",
description: "",
services: [],
attributes: [],
banners: null,
attachments: null,
},
onSubmit: async (values) => {
try {
const formData = new FormData();
let data = {};
Object.keys(values)?.forEach((property, key) => {
if (property != "banners" && property != "attachments") {
if (property == "category" || property == "subCategory") {
data[property] = { connect: [values[property]] };
} else {
data[property] = property.includes("Price")
? Number(values[property])
: values[property];
}
} else {
formData.append(`files.${property}`, values[property]);
}
});
formData.append("data", JSON.stringify(data));
const res = await fetch(
`${API_BASE_URL}/gigs`,
{
method: "POST",
body: formData,
headers: {
Authorization: "Bearer " + Cookies.get("token"),
},
}
);
console.log(res);
toast.success("Gig created successfully!");
} catch (error) {
toast.error("Couldn't create Gig");
}
},
});
Everything seems working fine except the banners
and attachments
.
However I can see that my files are being sent. It’s just that they are not being saved in Strapi.
Can you guys please help me find out what am I doing wrong here?