Fix strange error in pokedex fetch

This commit is contained in:
Dragon Fire
2020-03-23 14:38:38 -04:00
parent a2af169168
commit 045176f799
2 changed files with 10 additions and 4 deletions
+9 -4
View File
@@ -6,6 +6,7 @@ const missingno = require('../../assets/json/missingno');
module.exports = class PokemonStore extends Collection {
async fetch(query) {
query = encodeURIComponent(query.toLowerCase().replace(/ /g, '-').replace(/[^a-zA-Z0-9-]/g, ''));
if (!query) return null;
const num = Number.parseInt(query, 10);
if (this.has(num)) return this.get(num);
const found = this.find(pokemon => pokemon.slug === query);
@@ -15,9 +16,13 @@ module.exports = class PokemonStore extends Collection {
this.set(pokemon.id, pokemon);
return pokemon;
}
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
const pokemon = new Pokemon(body);
this.set(pokemon.id, pokemon);
return pokemon;
try {
const { body } = await request.get(`https://pokeapi.co/api/v2/pokemon-species/${query}/`);
const pokemon = new Pokemon(body);
this.set(pokemon.id, pokemon);
return pokemon;
} catch {
return null;
}
}
};