【问题标题】:TypeError: Cannot read property 'type' of undefined (redux toolkit)TypeError:无法读取未定义的属性“类型”(redux 工具包)
【发布时间】:2021-02-23 14:59:07
【问题描述】:

我正在尝试使用 redux 工具包获取一些数据,但它不起作用。我只是不断收到错误TypeError: Cannot read property 'type' of undefined。我正确设置了商店,因为我有其他减速器工作正常。但是当我尝试异步或获取数据时,我遇到了这个问题

错误:

App.js:

代码在const actionResult = await dispath(getLiveContest()) 处停止,之后不会在控制台记录任何内容。

  const dispatch = useDispatch();

  useEffect(() => {
    const fetchLiveContest = async () => {
      try {
        console.log(1);
        const actionResult = await dispatch(getLiveContest());
        console.log(2);
        const liveContest = unwrapResult(actionResult);
        console.log(liveContest);
      } catch (error) {
        console.log("Failed to fetch live contest: ", error);
      }
    };

    fetchLiveContest();
  }, []);

GetLiveContest():

这是函数的代码。我尝试了return {name: 'lala'},它仍然给了我类型错误

export const getLiveContest = createAsyncThunk(
  "contests/fetchLive",
  async (params, thunkAPI) => {
    console.log(thunkAPI, "thunkAPI");
    console.log(params);
    const liveContest = await axios ...

    return liveContest;
  }
);

幻灯片代码:

export const liveContestSlide = createSlice({
  name: "live",
  initialState: {
    contest: [],
    loading: "idle",
  },
  reducers: {},
  extraReducers: {
    // Add reducers for additional action types here, and handle loading state as needed
    [getLiveContest.fulfilled]: (state, action) => {
      // Add contest to the state array
      state.contest.push(action.payload);
    },
  },
});

我遵循了 redux 工具包文档。我还在stackoverflow上检查了其他问题,但仍然无法修复错误,请帮助

【问题讨论】:

  • 请发布整个代码而不是图像,因为它们难以阅读,我们无法复制粘贴它们来重现问题
  • 好的,我会修复

标签: async-await react-hooks redux-toolkit


【解决方案1】:

我只是将import getLiveContest from "./contestSlice"; 更改为import { getLiveContest } from "./contestSlice"; 并且它可以工作,结果我只是导入了错误的函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2021-11-20
    • 1970-01-01
    • 2023-02-15
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多