`How To Get User NFTs` - getNFTs() function throws error

I am following the How To Get User NFTs? All NFTs Owned by Address ( https://www.youtube.com/watch?v=S4rZ8EMTFaY )

After logging in as a user, I run the following line of code.

await Moralis.Web3.getNFTs({chain:"eth", address: '0xf56345338cb4cddaf915ebef3bfde63e70fe3053'});

After waiting about 60 seconds, it throws the following two error messages, which repeats about 5 times at intervals of 60 seconds.

Access to XMLHttpRequest at 'https://bemgqvt0f3ju.usemoralis.com:2053/server/functions/getNFTs' from origin 'http://127.0.0.1:5500' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

moralis.js:21917 
POST https://bemgqvt0f3ju.usemoralis.com:2053/server/functions/getNFTs net::ERR_FAILED

On the fifth round, it throws the same error messages, plus an extra one:

moralis.js:24019 
Uncaught Error: XMLHttpRequest failed: "Unable to connect to the Parse API"
    at handleError (moralis.js:22057)
    at async <anonymous>:1:1

I have tried it with several different addresses, and sometimes (for addresses with a small number of NFTs) it is succesful in returning the NFT details. When it is succesful, it returns the details within about 3 seconds. But most of the time, it throws the error messages above. For this address, (the boredelon account that has thousands of NFTs) I have never seen it return succesfully.

Environment details

Server: https://bemgqvt0f3ju.usemoralis.com:2053/server
Server Version: 0.0.241
Environment: Mainnet

Javascript code was run on my laptop, running a http server locally, on Chrome.

Hey @bobbington

Please share the full code. It looks like your Moralis.Web3.getNFTs is called several times

Hey @Yomoo

Here is the Code I have got for the html page.

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  </head>
  <body>
    <h1>NFT Demo</h1>
    <script>
        async function login() {
            Moralis.Web3.authenticate().then(function (user) {
                console.log('User logged in');
                console.log(`User address     : ${user.get('ethAddress')}`);
                console.log(`Username         : ${user.get('username')}`);                
            }
        )}
        async function getNFTs(options={chain:"eth"}){
            const nfts = await Moralis.Web3.getNFTs(options);
            return nfts;
        }
    </script>    
    <script>
      // connect to Moralis server
      Moralis.initialize("L0xThqKin5Q9je88tnAMtwx1jBaV8cL81yjx3BdN");
      Moralis.serverURL = "https://bemgqvt0f3ju.usemoralis.com:2053/server";
    </script>
</body>
</html>

In the console I call the functions manually.

  1. login

    login()
    
    • and then login through metamask when prompted, and wait for the printout with user address in the console.
  2. Get NFTs

    await Moralis.Web3.getNFTs({chain:"eth", address: '0xf56345338cb4cddaf915ebef3bfde63e70fe3053'});
    

This leads to the error messages I mentioned in the first post.

Hmmm, I’ve checked your code. It works fine.

Could you check it with button getNFTs?

<html>

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

<body>
  <h1>NFT Demo</h1>
  <button id="btn-getNFTs">getNFTs</button>
  <script>
    async function login() {
      Moralis.Web3.authenticate().then(function (user) {
        console.log('User logged in');
        console.log(`User address     : ${user.get('ethAddress')}`);
        console.log(`Username         : ${user.get('username')}`);
      })
    }
    getNFTs = async () => {
      const options = {
        chain: "matic",
        address: "0x8f3df14cb1ddb3b866cb1a82975ba7f300de8f49"
      }
      const nfts = await Moralis.Web3.getNFTs(options);
      console.log(nfts);
    }
    document.getElementById("btn-getNFTs").onclick = getNFTs;
  </script>
  <script>
    // connect to Moralis server
    Moralis.initialize("L0xThqKin5Q9je88tnAMtwx1jBaV8cL81yjx3BdN");
    Moralis.serverURL = "https://bemgqvt0f3ju.usemoralis.com:2053/server";
  </script>
</body>

</html>

@Yomoo , sometimes it works for the address and chain you provided:

const options = {
    chain: "matic",
    address: "0x8f3df14cb1ddb3b866cb1a82975ba7f300de8f49"
}

But most of the time it still throws the same error i have been mentioning.

And it still continues failing all the time for the eth address of bored elon

const options = {
    chain: "eth",
    address: "0xf56345338cb4cddaf915ebef3bfde63e70fe3053"
}

We are checking the issue with getNFT SDK function and will get back ASAP.

—————

In the meantime, we have another way of getting this information for ETH using the Deep Index API (support for other chains will come later as those NFT indexes are complete).

/nft/wallet/{owner_of} shows 951 NFTs for that bored elon address

nft/wallet/{address}/transfers shows 2777 NFT transfers

You can see all the Deep Index API endpoints on your Moralis Server by navigating to the “Deep Index API” page and viewing the Swagger docs there. See the “Getting Started” docs for the API here:

https://docs.moralis.io/deep-index-rest-api/deep-index-api

Would this be why my cloud functions all of a sudden stopped working?

I got a bit cocky and did alot of changes without checking everything but i was fairly sure it was all fine then all of a sudden everything stopped working LOL

Please create a new thread with your issue :raised_hands: post the code of your cloud functions there

This thread we keep for this specific issue we are discussing, your cloud function issue can be many different things

We have made changes to resolve this issue. You should be able to query it without any problem. Make sure to update your server to the latest version too.

Let me know if it worked ?

Happy BUIDLing! :smiley:

3 Likes

Amazing! I wasnt expecting a fix for this so quickly! Great work guys!!!

It works perfectly now :slight_smile: :slight_smile: :slight_smile:

1 Like