Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

72
Vistas
loop through array issue

I am calling an API that returns an array, i am trying to loop through the array but console is saying that the lenght is 0, never seen anything like it before and can not find out what is the issue here:

const funCall=async()=>{

      const userNFTsURLs = await prepareData();
      setNFTsUrls(userNFTsURLs);
}

  const prepareData = async () => {
    const res = await getUserNFTs();
    console.log(res);
/*returns 
0: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/4.json"
1: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/41.json"
2: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/45.json"
3: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/47.json"
4: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/49.json"
5: "ipfs://QmbNHPexuZWo3rnzAyyzZxTsAmMuN97R8Z7qwuPfEMXeX9/51.json"
length: 6
[[Prototype]]: Array(0)*/

    console.log(res.length);//returns 0


    res.map((rest: string) => console.log(rest));
    const test = res.map((url: string) => {
console.log(url)//does not return anything is not being called
      return url;
    });

    return res;
  };




getUserNFTs function, I just console logged the last forEach, it is loging it after it is logged on the component consuming it, it is very strange.

 const getUserNFTs = async () => {
    if (store.getState().blockChain.smartContract === null) {
      await connectToContract();
    }
    try {
      const balance = await getUserTokensBalance();
      if (balance <= 0) {
        return [];
      }
      let tokenIds = [];
      for (let i = 0; i < balance; i++) {
        const tokenId = await blockChain.smartContract.methods
          .tokenOfOwnerByIndex(walletId, i)
          .call();
        tokenIds.push(tokenId);
      }

      let tokensList: any = [];

      tokenIds.forEach(async (tokenId) => {
        const token = await blockChain.smartContract.methods
          .tokenURI(tokenId)
          .call();
        console.log("token", token);//logs it on console after logs al the other console logs

        tokensList.push(token);
      });

      return tokensList;
    } catch (error) {
      console.log(error);
    }
  };

what am I not getting here? I feel very frustrated after couple of hours trying to find out

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your problem is you're using forEach which is not waiting for results as you expected.

You should modify it to a usual for loop to get rid of async callback function in forEach.

for(const tokenId of tokenIds) {
        const token = await blockChain.smartContract.methods
          .tokenURI(tokenId)
          .call();
        console.log("token", token);//logs it on console after logs al the other console logs

        tokensList.push(token);
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda