Currently, I am utilizing the PokeAPI. My approach involves making fetch requests to obtain JSON data from a specific endpoint, and then attempting to parse and return it. The function responsible for this task can be located below:
function getPokemon(id){
pokemonData = {
name:"",
image:"",
id:id,
description:""
}
// Documentation: https://pokeapi.co/docs/v2#pokemon-species
fetch(`https://pokeapi.co/api/v2/pokemon-species/${id}/`)
.then((response) => response.json())
.then((data) => {
pokemonData.description = data.flavor_text_entries[0].flavor_text.toString()
}
)
// Documentation: https://pokeapi.co/docs/v2#pokemon
fetch(`https://pokeapi.co/api/v2/pokemon/${id}/`)
.then((response) => response.json())
.then((data) => {
pokemonData["image"] = data.sprites.other["official-artwork"].front_default.toString()
pokemonData["name"] = data.name.toString()
}
)
return pokemonData
}
After receiving the data, my attempts to access attributes show blank values, despite the object displaying the correct information:
https://i.sstatic.net/a15KY.png
I am uncertain about what is going wrong in this scenario. I have experimented with various attribute access formats such as data.name
versus data["name"]
, but none seem to have an impact. Any assistance on this matter would be greatly appreciated.