提问人:Kubilay 提问时间:10/28/2023 更新时间:10/28/2023 访问量:24
是否可以将服务器操作的结果发送到 Next.js 中的客户端组件?
Is it possible to send the results of server actions to a client component in Next.js?
问:
在 Next.js 13 中,我有一个 API,用于发布表单数据,我想在屏幕上的 Toast 中显示从后端发送的错误。如何用我的代码实现这一点?
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")};
答: 暂无答案
评论