Passing a current users wallet into the getnfts options

Code didnt copy right. I had to delete carrots off the front of a few lines to make it copy correctly. So it may look a little funny.

script src=“https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js”>

script src=“https://unpkg.com/moralis/dist/moralis.js”>

script src=".jquery.com/jquery-3.6.0.min.js">

Do these links look right? I took the http off the front of the third link because I can only post 2 links.

Links look ok to me.
Moralis.Web3API.account.getNFTs(); gets the address from current authenticated user as you don’t give it an address as a parameter.

Thanks for checking that cryptokid. How do you authenticate a user that is signing in? Here is the code I am using to log the user in.

script>

//connect to Moralis server

Moralis.initialize("lRCkcYjDbFtpxYwnA4un13UtUi7RqBfVtyjx5g0n"); // Application id from moralis.io

Moralis.serverURL = "https://kprjvwm59njt.moralisweb3.com:2053/server"; //Server url from moralis.io

//Display login button and let user login

async function login(){

  console.log("login clicked");

  var user = await Moralis.Web3.authenticate(); //wait for user to authenticate

  if(user){

    console.log(user); //print user to console

  }

    // get NFTs for current user on Mainnet

    async function getNFTs(){

     // const options = { chain: 'eth', address: 'ethereum.selectedAddress' };

      const nfts = await Moralis.Web3API.account.getNFTs();

    console.log(nfts)

                    

    nfts.forEach(function(nfts){

      let url = fixURL(nft.token_uri);

      fetch(url)

      .then(response => response.json())

      .then(data => {

          $("content").html($("content").html()+"<h2>"+data.name+"/h2");

          $("content").html($("content").html()+"<h3>"+data.description+"</h3>");

          $("#content").html($("#content").html()+"<img src='"+fixURL2(data.image)+"'></img>");

      

      });

    })

    }

function fixURL(url){

  if(url.startsWith("ipfs")){

    return "https://ipfs.moralis.io:2053/ipfs/"+url.split("ipfs://ipfs/").slice(-1)[0];

  }

  else{

    return url+"?format=json"

  }

}

}

getNFTs()

Looking at this code I have the impression that login function doesn’t have it’s ending } before a new getNFTs function starts to get defined.

A user is logged in when the login function gets executed

That definitely solved some of the problems. However now I am getting this error. From line 39: nfts.forEach(function(nfts){ Here is the error message:

riews.html:39 Uncaught (in promise) TypeError: nfts.forEach is not a function
at getNFTs (riews.html:39)

I tried adding a } after the getNFTs function but that just led me to getting the error below.

riews.html:39 Uncaught ReferenceError: nfts is not defined
at riews.html:39

1 Like

you can try to look at this example: How To Get User NFTs? All NFTs Owned by Address - @Ivan on Tech Explains

1 Like