服务器操作函数称为当前页面路由

server action function is called the current page route

提问人:Sai Krishnadas 提问时间:11/10/2023 最后编辑:Sai Krishnadas 更新时间:11/10/2023 访问量:5

问:

我有一个在表单提交时调用的服务器操作。

const addEmployee = async (e: FormData) => {
        "use server";
        const firstName = e.get("fNmae")?.toString();
        const lastName = e.get("lNmae")?.toString();
        const email = e.get("email")?.toString();
        const position = e.get("position")?.toString();
        const departmentId = e.get("department")?.toString();

        if (!firstName || !lastName || !email || !position || !departmentId) return;

        const newEmployee = {
            name: firstName + " " + lastName,
            email: email,
            position: position,
            departmentId: departmentId,
        };
        try {
            const response = await fetch("http://localhost:3000/employees/create", {
                method: "POST",
                body: JSON.stringify(newEmployee),
                headers: {
                    "Content-Type": "application/json",
                },
            });

            if (response.ok) {
                const data = await response.json();
                return data;
            } else {
                console.error("Failed to fetch employees data");
            }
        } catch (error) {
            console.error("Error while fetching employee data:", error);
        }
    };

数据应该发布到,但在检查网络选项卡时,函数会调用(此处组件位于记录页面中)。 为什么即使我提到过它也会打电话"http://localhost:3000/employees/create""http://localhost:3000/records""http://localhost:3000/record""http://localhost:3000/employees/create"

<form action={addEmployee}> 

这里是模态 UI,enter image description here

enter image description here

在上面的网络中, 它没有显示我上面传递的 JSON.stringfy 格式,它以不同的格式显示。

下一个.js 服务器操作

评论


答: 暂无答案