I want to access to the Pokémon types in the json file.
However, it looks like I can only retrieve name and url, any idea of what am I doing wrong?
const baseUrl = 'https://pokeapi.co/api/v2/pokemon/?limit=3000'
try {
fetch(baseUrl)
.then(response => {
const responseJson = response.json()
return responseJson
})
.then(data => {
const pokemons = data.results
pokemons.forEach(pokemon => {
console.log(pokemon);
})
})
.catch(error => {
console.error(error)
})
} catch (error) {
console.error(error)
}
Actually, this is how the API is done.
If you want to get more values about pokemons, you need to make another request by the given URL from the first result.
This can be done using another axios call inside the then
const baseUrl = 'https://pokeapi.co/api/v2/pokemon/?limit=1'
try {
fetch(baseUrl)
.then(response => {
const responseJson = response.json()
return responseJson
})
.then(async data => {
const pokemons = data.results
for (const pokemon of pokemons){
pokemon.data = await fetch(pokemon.url).then(res => res.json())
}
console.log(pokemons)
})
.catch(error => {
console.error(error)
})
} catch (error) {
console.error(error)
}
Note that the API is quite slow that's why I would recommand using pagination instead of limit=3000
ad you did.
This can be done using the offset
parameter which is the start of the pagination
For example, ?offset=20&limit=20
will get the 20 pokemons betwenn 20 and 40