I am fetching data from the Pokemon API and it seems to be able to fetch without error when I use the search function or just regular loading of the pokemon but when I filter the pokemon by type, the console shows many errors like
GET https://pokeapi.co/api/v2/pokemon/terapagos-stellar net::ERR_INSUFFICIENT_RESOURCES
This error occurs on both Brave Browser and Chromium but I do not experience this on Firefox.
Here is my code for the fetching of data:
useEffect(() => {
async function fetchPokemon() {
setIsLoading(true);
let url = URL;
if (filter) {
url = "https://pokeapi.co/api/v2/pokemon?limit=1302&offset=0";
}
const res = await fetch(url);
const data = await res.json();
const result = data.results;
setNext(data.next);
setPrev(data.previous);
const pokemonInfo = await Promise.all(
result.map(async (pokemon) => {
const res = await fetch(
`https://pokeapi.co/api/v2/pokemon/${pokemon.name}`
);
return res.json();
})
);
setIsLoading(false);
if (!search && !filter) {
setData(pokemonInfo);
}
}
fetchPokemon();
async function fetchPokemonByType(type) {
setData([]);
const res = await fetch(`https://pokeapi.co/api/v2/type/${type}`);
const data = await res.json();
const pokemonList =
data.pokemon?.map((pokemon) => pokemon.pokemon.name) || [];
const pokemonInfo = await Promise.all(
pokemonList.map(async (name) => {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`);
return res.json();
})
);
!search && setData(pokemonInfo);
}
fetchPokemonByType(filter);
async function searchPokemon() {
if (search !== "") {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${search}`);
if (res.ok) {
const data = await res.json();
setData([data]);
setSearchError("");
} else {
setSearchError("There is no such Pokémon");
setIsLoading(false);
}
}
}
searchPokemon();
}, [URL, filter, search]);
useEffect(() => {}, [search]);
The only thing that worked so far was disabling cache in dev tools.
Also tried incognito but didn’t work either
kfcezz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.