I cant seem to get the token address into a variable from the moralis result

this is my code anytime i try to get token addresses for nfts in a wallet it is always undefined

async function getsnft() {
nfts = await Moralis.Web3API.account.getNFTs();
const nftArray = [];
for (let i = 0; i < nfts.result.length; i++) {
console.log(JSON.stringify(nfts[i]));
try {
const nft = nfts[i].get(“token_address”); // This line
nftArray.push(nft)
}
catch(err) {
console.log(err);
continue;
}
}
return nftArray;
}

Here’s a little fix

async function getsnft() {
  const nfts = (
    await Moralis.Web3API.account.getNFTs({
      address: "",
      chain: "",
    })
  ).result;

  const nftArray = [];
  for (let i = 0; i < nfts.length; i++) {
    try {
      let nft = nfts[i]?.token_address;
      nftArray.push(nft);
    } catch (err) {
      console.log(err);
      continue;
    }
  }
  return nftArray;
}
2 Likes

thanks for this fix very helpful but
await Moralis.Web3API.account.getNFTs({
chain: “eth”
})
).result;

keeps on giving an undefined result even in console

His code works for getting an array of token_address, what address/chain are you using? How are you logging or trying to use the result?

...
console.log('nftArray', nftArray);
return nftArray;
1 Like
/* Moralis init code */
const serverUrl = "https://xz4dqimwwklr.usemoralis.com:2053/server";
const appId = "hvKueUPc9N0HbjKIulipg0TpkyHmto6FHT5SvfEf";
Moralis.start({ serverUrl, appId });


// To connect with connectwallet
async function connect1(){
  let user = Moralis.User.current();
  if (!user) {
    user = await Moralis.authenticate({
      signingMessage: "Log in using Moralis",
      provider: "walletconnect",
    })
      .then(function (user) {
        console.log("logged in user:", user);
        console.log(user.get("ethAddress"));
      })
      .catch(function (error) {
        console.log(error);
      });
      await Moralis.enableWeb3({
        provider: "walletconnect"
      })
  }
}


// To logout or refresh moralis
async function logOut() {
  await Moralis.User.logOut();
  console.log("logged out");
}


// Get NFTS in the wallet
async function getsnft() {
  const nfts = (
    await Moralis.Web3API.account.getNFTs()
  ).result;

  const nftArray = [];
  for (let i = 0; i < nfts.length; i++) {
    try {
      let nft = nfts[i]?.token_address;
      nftArray.push(nft);
    } catch (err) {
      console.log(err);
      continue;
    }
  }
  console.log('nftArray', nftArray); 
  return nftArray;
}


yes his code works well to get an array of token_address logged in console after running the function but i cant get to use the variables nfts,nft and nftarray after running the function … the results were not defined … this is my full code if you want to suggest a fix i want to be able to use the variables stored in nft and nft array in functions outside the getnft function … would appreciate the help

You don’t seem to be calling getsnft() anywhere. Where are you logging the results where they are not defined?

If you wanted to get the returned nftArray:

const result = await getsnft();

i want to be able to use the variables stored in nft and nft array

Do you mean nfts and nftArray? Then you could do:

return {
  nfts,
  nftArray,
};

...
// get data outside function
const result = await getsnft(); // can access result.nfts or result.nftArray

// or 

const { nfts, nftArray } = await getsnft();
1 Like

can you explain better i dont think i get the hang of it cause i still keep getting this errors and undefined

How are you trying to log it in code? What are the errors?

const { nfts, nftArray } = await getsnft(); This gets the returned nfts and nftArray variables from getsnfts(). After this, you can access these variables directly like console.log(nfts). You can use const result = await getsnft(); if this confuses you.

// your function
async function getsnft() {
  const nfts = (
    await Moralis.Web3API.account.getNFTs({
      address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d',
    })
  ).result;

  const nftArray = [];
  for (let i = 0; i < nfts.length; i++) {
    try {
      let nft = nfts[i]?.token_address;
      nftArray.push(nft);
    } catch (err) {
      console.log(err);
      continue;
    }
  }

  console.log('nftArray', nftArray);
  return {
    nfts,
    nftArray,
  };
}

...

// getting the getsnft data from another function
async function another() {
  const { nfts, nftArray } = await getsnft(); // get data
  console.log("another", nfts); 
}

thanks did work … any idea on how to run example transactions for each of the token address generated in the nftArray like for example the (Moralis.Web3API.account.getNFTsForContract()) but it would be run for each token address in the nftArray giving each result per token address … any help?

What do you mean by example transactions? How are you wanting to loop over each token address in nftArray? Do you want to use getNFTsForContract?

yes i want to use getnfts for contract … could you suggest a loop ? … i said example transactions cause im currently practicing and learning towards a project so i need an example to learn how to loop the tokenaddres from the array into the transaction

the array into the transaction

I’m still not understanding what you mean by transactions. When you use an API endpoint like getNFTs there’s no transactions taking place. Do you just mean data?

For getNFTsForContract, do you want to get all the data available? If you do, you’ll have to loop over multiple requests with cursor, otherwise you can only get the first 100 results.

There is probably a better/cleaner solution if you explain what you’re wanting to achieve with the API.