0

Does any one know why the javascript below function is not working

   <p>Juego: <span id="game"></span><br />
    Numero: <span id="num"></span><br />
    </p>
    <script>
    const api_url = 'https://example.com/answer'
    async function getResults() {
    const response = await fetch(api_url);
    const data = await response.json();
    const {gameID, results} = data;
    
    document.getElementById('game').textContent = gameID;
    document.getElementById('num').textContent = results;
    
    }
    getResults();
    </script>

In the website there is the following API information that it does not work it start with [] instead of {}

[
{
"gameID": 3,
"nameEN": "Pega 3 day",
"results": "346",
"formattedResults": "346",
"formattedPlus": null,
"formattedMultiplier": null
}
]

I have tried using the same code with other sites and it works.

<p>Latitude: <span id="lat"></span><br />
Longitude: <span id="lon"></span><br />
</p>
<script>
const api_url = 'https://api.wheretheiss.at/v1/satellites/25544'
async function getISS() {
const response = await fetch(api_url);
const data = await response.json();
const {latitude, longitude} = data;

document.getElementById('lat').textContent = latitude;
document.getElementById('lon').textContent = longitude;

}
getISS();
</script>

The only difference I notice is that it does not have the [ ] in the ones that work it have {} is that the API code is like

{
"name": "iss",
"id": 25544,
"latitude": 29.789981200076,
"longitude": -25.419752436897,
"altitude": 416.43990416292,
"velocity": 27597.685881734,
"visibility": "eclipsed",
"footprint": 4489.3067609254,
"timestamp": 1659308143,
"daynum": 2459792.4553588,
"solar_lat": 18.080118527986,
"solar_lon": 197.67255403362,
"units": "kilometers"
}
Phil
  • 157,677
  • 23
  • 242
  • 245
Manuel
  • 21
  • 3

1 Answers1

1
const {latitude, longitude} = data[0]  

@Konrad Linkowski

Manuel
  • 21
  • 3