重音如下所示:

export const registerUser = createAsyncThunk(
  "auth/register",
  async (userData) => {
    try {
      const response = await axios.post(REGISTER_URL, userData);
      return response.data;
    } catch (error) {
      // console.log(error.response.data) the output is provided below
      throw error.response.data;
    }
  }
);

输出:

{
  "error": "Bad Request",
  "message": [
    "email should not be empty",
    "email must be an email"
  ],
  "statusCode": 400
}

在给定这种输出格式的情况下,我应该如何在Reducer的.addMatcher()中构造state.error更新?

.addMatcher(
  (action) => action.type.endsWith("/rejected"),
  (state, action) => {
    state.loading = false;
    state.error = ?; // What's the appropriate way to structure the error messages?
  }
)

推荐答案

你可能真的应该把被拒绝的promise 和错误放在一起,而不是仅仅把它重新扔出go .有关更多详细信息,请参见Handling Thunk Errors.

示例:

export const registerUser = createAsyncThunk(
  "auth/register",
  async (userData, thunkAPI) => {
    try {
      const response = await axios.post(REGISTER_URL, userData);
      return response.data;
    } catch (error) {
      // console.log(error.response.data) the output is provided below
      return thunkAPI.rejectWithValue(error.response.data);
    }
  }
);

从现在开始,它将只在registerUser.rejected个动作的payload.

.addMatcher(
  (action) => action.type.endsWith("/rejected"),
  (state, action) => {
    state.loading = false;
    state.error = action.payload; // or pick out what you need from payload
  }
)

Javascript相关问答推荐

未使用Outlet渲染子组件

如何在dataTables PDF输出中正确渲染字形?

如何指定1条记录1个表?

如何保持子画布元素的1:1宽高比?

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

React Native平面列表自动滚动

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

我不知道为什么我的JavaScript没有验证我的表单

使用i18next在React中不重新加载翻译动态数据的问题

如何为我的astro页面中的相同组件自动创建不同的内容?

屏幕右侧屏障上的产卵点""

Regex结果包含额外的match/group,只带一个返回

还原器未正确更新状态

编辑文本无响应.onClick(扩展脚本)

在我的index.html页面上找不到我的Java脚本条形图

Reaction组件在本应被设置隐藏时仍显示

如何在和IF语句中使用||和&;&;?

在渲染turbo流之后滚动到元素

Socket.IO在刷新页面时执行函数两次