[SOLVED] Works slow while fetching NFTs

I am using the Moralis API in a project of our company. I need to fetch all the NFTs from a given wallet address on a particular chain using the getNFTs function. But it is taking much time to fetch NFTs and also while fetching some NFTs, 404 error occurs.

  • There are 76 Nfts to be fetched. But it takes almost 1 mins to fetch all of those.
  • Also within 76, some NFTs could not be fetched due to 404 error.

Is there any way to speed up and avoid this errror?

Here is my code.

const fetchNFTsUtils = async (data) => {
        let results = []
        for (const d of data) {
            if (d.token_uri) {
                let url = fixUrl(d.token_uri)
                try {
                    let res = await fetch(url)
                        .then(res => res.json())
                        .catch(err => null)
                    if (res && (res.name || res.image || res.id || res.description)) {
                        results.push(res)
                    }

                } catch (error) { }
            }
        }
       return results
}

let response = await Moralis.Web3API.account.getNFTs( { chain: chain, address: wa.address } )
let results = await fetchNFTsUtils(response.result)

maybe his part is slow:
let res = await fetch(url)
Try to run only let response = await Moralis.Web3API.account.getNFTs( { chain: chain, address: wa.address } ) and use console.log(response) to see how much time it takes.

@cryptokid , as you mentioned, I tried to fetch NFTs after removing that part. It takes less time. But I need to show NFT metadata. For that I have to run this code

let res = await fetch(url)

But it makes the thing slow. Is there any alternative way to fetch NFT metadata?

You can try: https://docs.moralis.io/moralis-server/web3-sdk/token#getnftmetadata

@cryptokid thanks. Now it is working fine.

1 Like