gpt4 book ai didi

async-await - 类型错误 : Cannot read property 'type' of undefined (redux toolkit)

转载 作者:行者123 更新时间:2023-12-05 08:22:08 24 4
gpt4 key购买 nike

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

错误:

enter image description here

应用程序.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():

这是函数的代码。我试图返回 {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 上检查了其他问题,但仍然无法修复错误,请帮忙

最佳答案

我只是将 import getLiveContest from "./contestSlice"; 更改为 import { getLiveContest } from "./contestSlice"; 它起作用了,原来我只是导入函数错误

关于async-await - 类型错误 : Cannot read property 'type' of undefined (redux toolkit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64789792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com