nextjs
runtime_error
ai_generated
true
错误:服务器操作失败。FormData 未定义或为空。
Error: Server Action failed. FormData is undefined or empty.
ID: nextjs/server-action-formdata-undefined
90%修复率
85%置信度
1证据数
2024-05-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
根因分析
服务器操作的 FormData 未定义,因为表单提交时没有事件处理程序,或者表单元素上缺少 action 属性。
English
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.
官方文档
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations解决方案
-
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.
无效尝试
常见但无效的做法:
-
60% 失败
'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% 失败
Server Actions require the form action attribute to properly bind FormData. onClick bypasses this and sends undefined.
-
70% 失败
Server Actions cannot be called directly from client components without 'use server' on the action function. This leads to undefined FormData.