In Next.js 13, I have an API where I post form data, and I want to display errors sent from the backend in a toast on the screen. How can I achieve this with my code?
const handleSubmit = async (FormData) => {
"use server";
try {
const session = await getServerSession(authOptions);
const response = await fetch(
`${Backend_URL}Products/CreateOneProductWithImage`,
{
method: "POST",
headers: {
Authorization: `Bearer ${session.accessToken}`,
},
body: FormData,
}
);
//Backend'den gelen erorların listesi.
let BackEndErros = await response.json();
console.log(BackEndErros);
error = BackEndErros;
if (
response.status === 201 ||
response.status === 200 ||
response.status.ok
) {
SuccesToast("Başarılı");
return { message: "Success!" };
} else {
BackEndErros &&
BackEndErros.map((item, key) => {
return ErrorToast(
item.value.length > 2 ? item.value[1] : item.value[0],
key
);
});
return { message: "There was an error." };
}
} catch (error) {
console.error("An error occurred:", error);
}
revalidateTag("Products")};