0

i use cloud code in parseserver - Back4app, that uses node.js to make a request for Football API. I got the following result (resultado) with the axios request:

{
    "result": {
        "get": "teams",
        "parameters": {
            "country": "brazil"
        },
        "errors": [],
        "results": 785,
        "paging": {
            "current": 1,
            "total": 1
        },
        "response": [
            {
                "team": {
                    "id": 6,
                    "name": "Brazil",
                    "code": "BRA",
                    "country": "Brazil",
                    "founded": 1914,
                    "national": true,
                    "logo": "https://media.api-sports.io/football/teams/6.png"
                },
                "venue": {
                    "id": 204,
                    "name": "Estadio Jornalista Mário Filho (Maracanã)",
                    "address": "Rua Professor Eurico Rabelo, Maracanã",
                    "city": "Rio de Janeiro, Rio de Janeiro",
                    "capacity": 78838,
                    "surface": "grass",
                    "image": "https://media.api-sports.io/football/venues/204.png"
                }
            },
            {
                "team": {
                    "id": 118,
                    "name": "Bahia",
                    "code": "BAH",
                    "country": "Brazil",
                    "founded": 1931,
                    "national": false,
                    "logo": "https://media.api-sports.io/football/teams/118.png"
                },
                "venue": {
                    "id": 216,
                    "name": "Arena Fonte Nova",
                    "address": "Rua Lions Club, Nazaré",
                    "city": "Salvador, Bahia",
                    "capacity": 56500,
                    "surface": "grass",
                    "image": "https://media.api-sports.io/football/venues/216.png"
                }
            }, (repeat for more 783 X)

How can i loop the response and get all "teams" and "venues" names, id, adress, etc.. using Javascript.

i have tried many ways, but none of them worked for me.

follow my code:

Parse.Cloud.define("teams", (request)=>{
  const axios = require("axios");
  const time = request.params.time;
  const options = {
    method: 'GET',
    url: 'https://api-football-v1.p.rapidapi.com/v3/teams',
    params: {country:'brazil'},
    headers: {
      'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
      'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
     }

  };
      var resultado = axios.request(options).then( function (response) {
        return (response.data);
      //  return "olá função "
    }).catch(function (error) {
         return("erro meu nobre!");
});

return resultado;
Barmar
  • 741,623
  • 53
  • 500
  • 612
Duda Clash
  • 19
  • 2
  • `resultado.then(data => data.result.response.forEach(...))` – Barmar Nov 22 '22 at 19:47
  • hi,@Barmar. Would you mind to complete the forEach(..) above? i tried to use forEach(response=>{const idTeam = team.id; //and so on. – Duda Clash Nov 23 '22 at 10:59
  • sorry but i'm still learning javascript. – Duda Clash Nov 23 '22 at 11:11
  • 1
    It depends on what you want to do with the results. See the answer by Lludolfyn for a simple example. You can replace `console.log(team)` with something that adds a row to a table using the object properties. – Barmar Nov 23 '22 at 16:07
  • See https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json for more information about accessing nested properties. – Barmar Nov 23 '22 at 16:08
  • perfect. i adjusted to save in a parse table the details that i want. – Duda Clash Nov 23 '22 at 19:29

1 Answers1

0

// const axios = require("axios");
const options = {
  method: 'GET',
  url: 'https://api-football-v1.p.rapidapi.com/v3/teams',
  params: {
    country: 'brazil'
  },
  headers: {
    'X-RapidAPI-Key': 'd69302225emshdf770c890926efdp19ca04jsn08d2244e2253',
    'X-RapidAPI-Host': 'api-football-v1.p.rapidapi.com',
  }
};
const resultado = axios.request(options)
  .then(function(response) {
    const data = response.data.response 
    data.forEach(function(team) {
      console.log(team) // "team" variable here contains the "team" and "venue" objects for each team
    })
  })
  .catch(function(error) {
    return ("erro meu nobre!");
  })
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
Ludolfyn
  • 1,806
  • 14
  • 20
  • Perfect!. I adjusted your and @ Lludolfyn answers, to save in a parse class table the details that i want. thanks very much you both.✔ – Duda Clash Nov 23 '22 at 19:56
  • Awesoooome! I'm glad it worked out @DudaClash! If this is the accepted answer, would you mind marking it as the accepted answer? – Ludolfyn Nov 23 '22 at 21:41