axios 解析服务器API请求返回空{}

fcy6dtqo  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(101)

我使用Back 4app,它使用Parse Server和node.js(axios模块)来实现对API-football的调用。我得到了一个200成功的响应代码,运行如下的模板代码,但它总是返回一个空的{}响应(失眠)。提前感谢您的帮助。

Parse.Cloud.define("next50", (request)=>{
  const axios = require("axios");

  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/fixtures',
    params: {next:'50'},
    headers: {
       'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
       'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
     }

  };
  axios.request(options).then( function (response) {
        return response.data;
    }).catch(function (error) {
         return error;
});
});
yhived7q

yhived7q1#

我仔细阅读了axios文档,并实现了一个有效的代码,返回正确的JSON响应。

Parse.Cloud.define("next50", (request)=>{
  const axios = require("axios");

  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/fixtures',
    params: {next:'50'},
    headers: {
      'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
      'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
     }

  };
      var result = axios.request(options).then( function (response) {
        return (response.data);
     
    }).catch(function (error) {
         return(error);
});
    return (result);//👍✔
});

以下是回应:

{
    "result": {
        "get": "fixtures",
        "parameters": {
            "next": "50"
        },
        "errors": [],
        "results": 50,
        "paging": {
            "current": 1,
            "total": 1
        },
        "response": [
            {
                "fixture": {
                    "id": 841439,
                    "referee": null,
                    "timezone": "UTC",
                    "date": "2022-11-06T11:00:00+00:00",
                    "timestamp": 1667732400,
                    "periods": {
                        "first": null,
                        "second": null
                    },
                    "venue": {
                        "id": 7699,
                        "name": "Salaspils sporta nama stadions",
                        "city": "Salaspils"
                    },
                    "status": {
                        "long": "Not Started",
                        "short": "NS",
                        "elapsed": null
                    }
                },
                "league": {
                    "id": 365,
                    "name": "Virsliga",
                    "country": "Latvia",
                    "logo": "https://media.api-sports.io/football/leagues/365.png",
                    "flag": "https://media.api-sports.io/flags/lv.svg",
                    "season": 2022,
                    "round": "Regular Season - 35"
                },
                "teams": {
                    "home": {
                        "id": 4143,
                        "name": "Super Nova",
                        "logo": "https://media.api-sports.io/football/teams/4143.png",
                        "winner": null
                    },
                    "away": {
                        "id": 4153,
                        "name": "Valmiera / BSS",
                        "logo": "https://media.api-sports.io/football/teams/4153.png",
                        "winner": null
                    }
                },
                "goals": {
                    "home": null,
                    "away": null
                },
                "score": {
                    "halftime": {
                        "home": null,
                        "away": null
                    },
                    "fulltime": {
                        "home": null,
                        "away": null
                    },
                    "extratime": {
                        "home": null,
                        "away": null
                    },
                    "penalty": {
                        "home": null,
                        "away": null
                    }
                }
            },
            {
                "fixture": {
                    "id": 841440,
                    "referee": null,
                    "timezone": "UTC",
                    "date": "2022-11-06T11:00:00+00:00",
                    "timestamp": 1667732400,
                    "periods": {
                        "first": null,
                        "second": null
                    },
                    "venue": {
                        "id": null,
                        "name": "LNK Sporta Parks",
                        "city": "Riga"
                    },
                    "status": {
                        "long": "Not Started",
                        "short": "NS",
                        "elapsed": null
                    }
                },
                "league": {
                    "id": 365,
                    "name": "Virsliga",
                    "country": "Latvia",
                    "logo": "https://media.api-sports.io/football/leagues/365.png",
                    "flag": "https://media.api-sports.io/flags/lv.svg",
                    "season": 2022,
                    "round": "Regular Season - 35"
                },
                "teams": {
                    "home": {
                        "id": 4160,
                        "name": "Rīgas FS",
                        "logo": "https://media.api-sports.io/football/teams/4160.png",
                        "winner": null
                    },
                    "away": {
                        "id": 661,
                        "name": "FK Liepaja",
                        "logo": "https://media.api-sports.io/football/teams/661.png",
                        "winner": null
                    }
                },
                "goals": {
                    "home": null,
                    "away": null
                },
                "score": {
                    "halftime": {
                        "home": null,
                        "away": null
                    },
                    "fulltime": {
                        "home": null,
                        "away": null
                    },
                    "extratime": {
                        "home": null,
                        "away": null
                    },
                    "penalty": {
                        "home": null,
                        "away": null
                    }
                }
            },
            {

向所有人致以最诚挚的问候。

相关问题