I trying to do a pokedex using react and pokeAPI but when i try to save the response on a constant and return it this always return undefined. c
try {
const pokemonResponse = getPokemonByName({
pokemonName,
})
console.log(pokemonResponse)
return pokemonResponse
} catch (e) {
throw new Error("Ups! We had a problem with product's fetch. Details: " e)
}
}
here a codesandbox with the project
https://codesandbox.io/s/compassionate-haslett-ngv7z?file=/src/data/storage/PokemonStorage.ts
CodePudding user response:
I found 2 issues in the code. See my fork on Codesandbox.
getPokemonByNamereturned undefined (because it didn't return anything). It should returnPromisecontaining response from API I assume. Compare it togetAllPokemonsfunction in the same file that actually returnsPromise. Make sure you understand the arrow functions syntax.- Now you need to add
awaitwhen callinggetPokemonByNameinasyncfunction. This will correctly assign result topokemonResponse. See docs on async functions.
