Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

71
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda