nextjs
runtime_error
ai_generated
true
Error: Server Action failed. FormData is undefined or empty.
ID: nextjs/server-action-formdata-undefined
90%Fix Rate
85%Confidence
1Evidence
2024-05-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
Root Cause
Server Action's FormData is undefined because the form was submitted without an event handler or the action prop is missing on the form element.
generic中文
服务器操作的 FormData 未定义,因为表单提交时没有事件处理程序,或者表单元素上缺少 action 属性。
Official Documentation
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutationsWorkarounds
-
95% success Ensure the form has an action prop pointing to the Server Action function, and the action is defined in a separate file or with 'use server' at the top of the action function.
Ensure the form has an action prop pointing to the Server Action function, and the action is defined in a separate file or with 'use server' at the top of the action function.
-
80% success Use the experimental useActionState hook with a client component to handle the form submission manually if the form must be a client component.
Use the experimental useActionState hook with a client component to handle the form submission manually if the form must be a client component.
中文步骤
Ensure the form has an action prop pointing to the Server Action function, and the action is defined in a separate file or with 'use server' at the top of the action function.
Use the experimental useActionState hook with a client component to handle the form submission manually if the form must be a client component.
Dead Ends
Common approaches that don't work:
-
60% fail
'use server' should be on the Server Action function, not the component file. This causes the form to not recognize the action as a server action.
-
80% fail
Server Actions require the form action attribute to properly bind FormData. onClick bypasses this and sends undefined.
-
70% fail
Server Actions cannot be called directly from client components without 'use server' on the action function. This leads to undefined FormData.