nextjs runtime_error ai_generated true

错误:服务器操作失败。FormData 未定义或为空。

Error: Server Action failed. FormData is undefined or empty.

ID: nextjs/server-action-formdata-undefined

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations

解决方案

  1. 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.
  2. Use the experimental useActionState hook with a client component to handle the form submission manually if the form must be a client component.

无效尝试

常见但无效的做法:

  1. 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.

  2. 80% 失败

    Server Actions require the form action attribute to properly bind FormData. onClick bypasses this and sends undefined.

  3. 70% 失败

    Server Actions cannot be called directly from client components without 'use server' on the action function. This leads to undefined FormData.