[SOLVED] getNFTsForContract forEach

Hi, i made the GetNFTs, got it working, then i wanted to getNFTsForContract, and i can’t get it to work.

I get this error

Uncaught (in promise) TypeError: nfts.forEach is not a function at getNFTsForContract

What am i doing wrong here ?

Got it solved… Solution was .result before .forEach, like this .:

nfts.result.forEach(function (nft)

2 Likes

i am having the same issue
try.html:43 Uncaught (in promise) TypeError: allNFTs.forEach is not a function
at getNFTs

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

NFT Explorer

<p>
  Address: <input type="text" id="address" size="64" maxlength='64' /><br />
  <button id="btnUpdate">Update</button>
</p>

<h3>Content</h3>
<div id="content"></div>
<script>
   
    const serverUrl = "ur server";
    const appId = "ur app";
    Moralis.start({ serverUrl, appId });

    async function getNFTs(chain, address) {
     
      const options = { chain: chain, address: address };
      const nftCount = await Moralis.Web3API.account.getNFTsCount(options);
      $("#content").html("<p>Items count: " + nftCount + " </p>");
      if (nftCount > 0) {
        const allNFTs = await Moralis.Web3API.account.getNFTs(options);
        console.log(allNFTs);
        allNFTs.forEach( (nft) => {
          let url=fixURL(nft.token_uri);
            fetch(url)
              .then(response => response.json())
              .then(data => {
                $("#content").html($("#content").html() 
                  + "<div><img width='100' align='left' src='" + fixURL(data.image) + "' />"
                  + "<h2>" + data.name + "</h2>"
                  + "<p>" + data.description + "</p></div><br clear='all' />");
              });
        });
      }
    }

    fixURL = (url) => {
      if (url.startsWith("ipfs")) {
        return "https://ipfs.moralis.io:2053/ipfs/" + url.split("ipfs://ipfs/")[1];
      } else {
        return url + "?format=json";
      }
    }

    document.getElementById("btnUpdate").onclick = () => {
      console.log("Update!");
      let chain = $("#chain").val();
      let address = $("#address").val();
      console.log("Update! chain="+chain+" address="+address);
      if (typeof chain !== 'undefined' && typeof address != 'undefined') {
        getNFTs(chain, address);
      }
    }
  </script>
</body>

I am unable to display the nfts can someone guide me ?

what you see in that console.log output?

I want to display the name ,description and image of the nft below the item count on the page.
Why isn’t it displaying?

it looks like it stops at that error with startsWith, a variable could be undefined and all the code will stop at that error

I don’t know what’s wrong with it.I also tried to update the code with newest sdk version but it also gives error


The getNftsCount function works when using the old version