未处理的拒绝(类型错误):无法读取属性

时间:2021-02-13 19:01:33

标签: javascript reactjs api redux axios

我确定我的api已经成功连接到popularGamesURL upcomingGamesURLnewGamesURL

当我尝试获取我的 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;

我试图找出错误。

请提出任何建议。

1 个答案:

答案 0 :(得分:1)

您在 paylaod 和您期望在您的减速器中有一个名为 gamesAction.js 的属性。

payload

一个简单的错字,您应该拥有在本地调试自己的所有方法。