Home > Mobile >  i cannot save a response after execute the request using axios Instance (Pokedex - PokeApi)
i cannot save a response after execute the request using axios Instance (Pokedex - PokeApi)

Time:01-12

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.

  1. getPokemonByName returned undefined (because it didn't return anything). It should return Promise containing response from API I assume. Compare it to getAllPokemons function in the same file that actually returns Promise. Make sure you understand the arrow functions syntax.
  2. Now you need to add await when calling getPokemonByName in async function. This will correctly assign result to pokemonResponse. See docs on async functions.
  •  Tags:  
  • Related