Next.js Discord

Discord Forum

Send Array of Object with Form Data

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
this is my code
async function onSubmit(values: createTourFormValues) { const formData = new FormData(); Object.entries(values).forEach(([key, value]) => { if (Array.isArray(value)) { const dataGalletry = value.forEach((gallery) => { Object.entries(gallery).forEach(([subKey, subValue]) => { formData.append(subKey, subValue); }); }); } else if (value instanceof Date) { formData.append(key, value.toString()); } else { formData.append(key, value); } }); try { await createTour(formData); toast({ className: "bg-green text-white font-semiBold", description: "Tour created successfully", }); } catch (error) { toast({ className: "bg-red text-white font-semiBold", description: "Something went wrong, please try again.", }); } }

i want to return like this

values: {
body: string;
title: string;
checkIn: Date;
checkOut: Date;
guests: string;
pricePerNight: string;
galleryTour: {
titleGallery?: string | undefined;
imageGallery?: File | undefined;
}[];
feauturedImage?: File | undefined;
}

0 Replies