在Reaction-Router中,当表单提交时,我使用action向后端发送POST请求.这些都是正常工作的问题是我想返回响应和来自后端的错误.然后,我在AXIOS和CATCH函数中返回这些函数,但在UI中它显示为未定义.

export async function registerAction({ request }) {
  const formData = await request.formData();

  const response = await apiClient.get("/sanctum/csrf-cookie").then((response) => {
    apiClient
      .post("/register", {
        first_name: formData.get("first_name"),
        last_name: formData.get("last_name"),
        full_name: formData.get("full_name"),
        email: formData.get("email"),
        //birthday: formData.get("birthday"),
        address: formData.get("home_address"),
        phone_no: formData.get("phone_number"),
        ssn: formData.get("ssn"),
        password: formData.get("password"),
        password_confirmation: formData.get("password_confirmation"),
      })
      .then((response) => {
        return response;
      })
      .catch((errors) => {
        return errors;
      });
  });

  return null;
}

推荐答案

registerAction操作函数只是将null返回给用户界面.它应该返回AXIOS请求的结果,例如Promise Chain.

示例:

export async function registerAction({ request }) {
  const formData = await request.formData();

  return apiClient.get("/sanctum/csrf-cookie")
    .then(async (response) => {
      await apiClient.post("/register", {
        first_name: formData.get("first_name"),
        last_name: formData.get("last_name"),
        full_name: formData.get("full_name"),
        email: formData.get("email"),
        //birthday: formData.get("birthday"),
        address: formData.get("home_address"),
        phone_no: formData.get("phone_number"),
        ssn: formData.get("ssn"),
        password: formData.get("password"),
        password_confirmation: formData.get("password_confirmation"),
      });
      return response;
    })
    .catch((error) => {
      return error;
    });
  });
}

或直接使用async/await/try/catch.

export async function registerAction({ request }) {
  const formData = await request.formData();

  try {
    const response = await apiClient.get("/sanctum/csrf-cookie");
    await apiClient.post("/register", {
      first_name: formData.get("first_name"),
      last_name: formData.get("last_name"),
      full_name: formData.get("full_name"),
      email: formData.get("email"),
      //birthday: formData.get("birthday"),
      address: formData.get("home_address"),
      phone_no: formData.get("phone_number"),
      ssn: formData.get("ssn"),
      password: formData.get("password"),
      password_confirmation: formData.get("password_confirmation"),
    });
    return response;
  } catch(error) {
    return error;
  };
}

Reactjs相关问答推荐

CSS转换不适用于React MUI对象

如何在Pivot模式下自定义标题?

无法在jest中发布行动

如何创建react router v6的自定义子路由?

单击空白区域时,Reaction Multiple下拉组件不关闭

为多租户SSO登录配置Azure AD应用程序

有没有办法将MUI网格元素与Flex-Start对齐?

无法在REACTION TANSTACK查询中传递参数

阻止组件更新的多分派Redux Thunk或setState是否有任何问题

Mui 在 TextField 标签上添加工具提示显示两个工具提示框

React - 将一个充满空值的无限大小的数组存储在 ref 中是否可以,或者我应该重置数组吗?

如何使用 redux 和 react-plotly.js

虽然通过了身份认证,但并没有导航到请求的页面

MUI - ButtonGroup fullWidth 条件屏幕大小不起作用?

RTK 查询Mutations 在 StrictMode 中执行两次

无法读取未定义的react native 的属性参数

在 Formik 表单中访问子组件的值

ReactJs 行和列

使用 yarn 编译 protobuf js 时出现错误pbjs: command not found

需要帮助在 document.queryselector 的 nextjs 页面上插入 useEffect 状态