首页 > 解决方案 > 我正在拨打我的 api 电话,但我无法将其返回给我的动作创建者

问题描述

我有一个动作创建者,它调用我的 apI(这是它自己的文件),api 工作,动作创建者也工作。我能够 console.log 来自 api 的响应,我的问题是将它返回给我的动作创建者,在那里进行了 api 函数调用。谢谢你。

**ACTION CREATOR**

import * as types from './actionTypes';
import FootballApi from '../api/footballApi';
import { loadLeag_Success } from '.';



//type
export const LOAD_LEAG_GAMES_SUCCESS = 'LEAGUE_GAMES_SUCCESS';

export function loadLeag_Game_Success(data){
    //console.log("before dis", data);
    return;
};


export function getLeaguesGames(idArr, date) { 
    return function(dispatch) {
        Promise.all(FootballApi.getLeaguesGamesAPI(idArr, date))
        .then(() => {
// .        I WANT TO RETURN IT HERE.
               console.log("returned")
               
            })
        //      .catch(error => {
        //         throw(error)
        //    })
    };
}

**API FILE**

export default class FootballApi {  
  static getAllLeags() {
    return fetch('https://apifootball.com/api/?APIkey=42f53c25607596901bc6726d6d83c3ebf7376068ff89181d25a1bba477149480&action=get_leagues').then(response => {
         return response.json();
      }).catch(error => {
        return error;
    });
  }

    static getLeaguesGamesAPI(idArr, date){
    
      return idArr.map((id)=>{
        return fetch(`https://apifootball.com/api/?APIkey=42f53c25607596901bc6726d6d83c3ebf7376068ff89181d25a1bba477149480&action=get_events&from=${date}&to=${date}&league_id=${id}`)
        .then(res =>{
          return res.json();
        })
          .then((game) => {
            if(!game.error){
              //console.log("game")
              return game;
          }
        })
      })
    }
};

标签: javascriptreactjsreact-nativemobilereact-redux

解决方案


我想通了,我删除了-

   .then((game) => {
        if(!game.error){
         //console.log("game")
      }
     })

并将其放入我的动作创建器中。一切都按我需要的方式工作。谢谢。


推荐阅读