【发布时间】:2021-05-17 04:55:34
【问题描述】:
我确定我的 api 已成功连接 popularGamesURL upcomingGamesURL 和 newGamesURL
当我尝试获取我的 api 时显示错误:
Unhandled Rejection (TypeError): Cannot read property 'popular' of undefined
我试过这种方法
gamesAction.js文件:
import axios from "axios";
import { popularGamesURL, upcomingGamesURL, newGamesURL } from "../api";
//Action creator
export const loadGames = () => async (dispatch) => {
//Fetch axios
const popularData = await axios.get(popularGamesURL());
const newGamesData = await axios.get(newGamesURL());
const upcomingData = await axios.get(upcomingGamesURL());
dispatch({
type: "FETCH_GAMES",
paylaod: {
popular: popularData.data.results,
upcoming: upcomingData.data.results,
newGames: newGamesData.data.results,
},
});
};
gamesReducer.js文件:
const initialState = {
popular: [],
newGames: [],
upcoming: [],
searched: [],
};
const gamesReducer = (state = initialState, action) => {
switch (action.type) {
case "FETCH_GAMES":
return {
...state,
popular: action.payload.popular,
upcoming: action.payload.upcoming,
newGames: action.payload.newGames,
};
default:
return {
...state,
};
}
};
export default gamesReducer;
我试图找出错误。
请给点建议。
【问题讨论】:
标签: javascript reactjs api redux axios