0

by hobbie I have created a pokedex where I want to register the shiny pokemons that I have captured, I have them saved in google spreadsheets, and I want to capture the data with JSon

I based myself on a youtube video (link below), and I modified some things, adding google spreadsheets to read the captured ones

This link have the project if you wanna see all: https://codepen.io/willy587/pen/pojNLGg

Ok the problem is when a want to read my spreadsheets i have this:

async function getPokemonStatus(id){
    const response = await 
fetch(`https://spreadsheets.google.com/feeds/list/1x5Nhf8T4kBGxyLxCskHUNlF0_PScXrrl3Wrc5C7PDhc/od6/public/values?alt=json`);
    const data = await response.json();
    const pkstatus = data.feed.entry[id].gsx$estado.$t;
    console.log(pkstatus);//<--- show the status and is fine, works in that point 
    return pkstatus;//<------Here I try return the status because I want save in a variable/const 
} 

if (pokemon.id) {
   pokemonEl.innerHTML = pokeInnerHTML2; //Here is the sprite of the pokemon
   const status = getPokemonStatus(pokemon.id);// <---- When I do this save [Object Promise] {}
   console.log(status);
} else {
   pokemonEl.innerHTML = pokeInnerHTML; //Here is the missigno sprite, if I don't have the pokemon
}


I try use .then method, but it doesn't work either I look for others examples in the web but nothing I'm not a pro in javascript is my first code so if you can help me, I'll be grateful

you can see All the code in the codepen link, if you wanna work you there, Thanks

base code video https://www.youtube.com/watch?v=XL68br6JyYs

google spread:https://docs.google.com/spreadsheets/d/1x5Nhf8T4kBGxyLxCskHUNlF0_PScXrrl3Wrc5C7PDhc/edit#gid=0

this google spread is a example, the true google spread is another with the 800+ pokemon

  • async functions return promises, you would need use `async/await` or `then` on the `getPokemonStatus()`. such as `getPokemonStatus(pokemon.id).then((status) => { console.log(status); });` – Alexander Staroselsky Apr 22 '20 at 20:44
  • I tried that and doesn´t work, because I want compare status value `if (status == "Libre")` – Guillermo Ramirez Apr 22 '20 at 21:19
  • You can't simply return static, resolved values from async functions or similar functions that return promises. Either execute the code that consumes this in an async function and await the resolved value or do it all in `then()`. – Alexander Staroselsky Apr 22 '20 at 21:21
  • it's better if you add your `.then` implementation, so that we can be certain, whether you are using it correctly. – Merve Sahin Apr 22 '20 at 22:02

1 Answers1

0

Thanks well I do this, put all the code into a then. like Alexander Staroselky said

 async function getPokemonStatus(id){
    const response = await fetch(`https://spreadsheets.google.com/feeds/list/1x5Nhf8T4kBGxyLxCskHUNlF0_PScXrrl3Wrc5C7PDhc/od6/public/values?alt=json`);
    const data = await response.json();
    const pkstatus = data.feed.entry[id].gsx$estado.$t;
  return pkstatus;
} 

if (pokemon.id) {
   getPokemonStatus(pokemon.id-1).then((status) => { 

 if (status == "Capturado")
{
  pokemonEl.innerHTML = pokeInnerHTML2;
  //console.log(getPokemonStatus(pokemon.id));
} 
  else
{
   pokemonEl.innerHTML = pokeInnerHTML;
}

   });