[SOLVED]Fetch token_uri returning undefined

Trying to fetch(token_uril)

Like so:

async function getNFTs() {
    const queryAll = new Moralis.Query("PolygonNFTOwners");
    queryAll.equalTo("owner_of", ethereum.selectedAddress);
    const data = await queryAll.find();
    const nftArray = [];
    for (let i = 0; i < data.length; i++) {
        console.log(JSON.stringify(data[i]));
        try {
            const metadataInfo = await fetch(data[i]["token_uri"]); // This line
            const metadata = await metadataInfo.json();
            const nft = { "object_id": data[i].id, "token_id": data[i]["token_id"], "token_uri": data[i]["token_uri"], "contract_type": data[i]["contract_type"], "token_address": data[i]["token_address"], "image": metadata["image"], "name": metadata["name"], "description": metadata["description"] }
            nftArray.push(nft)
        }
        catch(err) {
            console.log(err);
            continue;
        }
    }
    return nftArray;
}

Having an issue with this line.

const metadataInfo = await fetch(data[i]["token_uri"]); 

it’s using the inbuilt fetch not a bespoke function.

Could you log that url that it tries to fetch? From that error it looks like it is not accessible.

It is just how it is formatted in the console.

https://ipfs.moralis.io:2053/ipfs/QmUayXZBLrQzywAesxiyreJMTaBuvCe4RJea3QCUD7ibEu

Are you sure the problem is form this url?

Changed

data[i]["token_uri"]

to

data[i].get("token_uri")

Worked fine.

Found some syntax errors later down the line.

Fixed them.

Now up and running.

Appreciate the support!

Marking as solved.

1 Like