I do not know where i have gone wrong

What is the error that it says

````

`` ![image|375x500](upload://ojJ7Y2pKYDLz6N3Y31T44uh6vqG.jpeg)

does it give an error on compilation? or only some kind of warning in the interface?

i havent compile it yet… i am just wondering while i am getting the error message

hey, try to make a function in the use effect and call it there, it is the same thing instead of what you are doing (for some cases react might not like that) eg

useEffect(
  call();
async function call(){
// your code 
}
)
// the if then your dependencies array (something like this)

and the chain may not be accepted seeing that it is capitalised, try using the hex decimal for the chain.
i dont know if the .then is needed, you can just set it without the .then if it isnt needed
and do you have the ethers library installed ? moralis also has a way of doing something like that
in (https://docs.moralis.io/moralis-server/tools/moralis-units#converting-token-value-from-wei)

//Convert token value to ETH style with 18 decimals
const tokenValue = Moralis.Units.FromWei("2000000000000000000", 18)

and i can really see the errors clearly, maybe you can copy and paste it ?

2 Likes

and what value is res ?
Because i see you are indexing it ? have you console log and see what it returns ?
from the looks of thing, res shouldnt be used that way (i can only see part of your code so i am not 100% sure), maybe you are trying to get the balance of a queried address, shouldn’t tokenMetadata be the one been indexed, as it should return an array with a list of tokens ?
and maybe you can set up a null test in case it is empty

yes i am  trying to get the balance of a queried address

i just listed the steps above, but if you are finding it hard or a bit confusing , post the code and errors (so i can copy and paste) pic arent really good at doing that :sweat_smile:

you may also need to console log tokenMetadata to see how you will access the balance and you may also need to loop to get all the balance (except if you just want the balance at index 0)

useEffect(()=>{
    balance();
    async function balance (){
    query.equalTo("tokenId", props.data.saveData.tokenId);
    const dd = await query.find();
    //const getAmount = JSON.parse(JSON.stringify(dd));
    const getAmount = JSON.parse(dd); // if this is not ok, you can revert back to the former way commented
    setPostData(getAmount);
    const options = { chain: "0xa869", address: user.attributes.account }; // avalanche testnet
    // find list of hex for chains here (https://docs.moralis.io/moralis-server/web3-sdk/intro#supported-chains)
    const tokenMetadata = await Moralis.Web3API.account.getTokenBalances(options)
    console.log(JSON.parse(tokenMetadata));
// you may want to convert the tokenMetadata to a json with JSON.parse and store it in a hook 
    setBal(Moralis.Units.FromWei("you can then do JSON.parse(tokenMetadata).result[0].balance", 18));
    // depending on what is returned in the console.log, you can also store (JSON.parse(tokenMetadata)) in a hook and then access it with the hook e.t.c
    // you can also do a null check to check if (JSON.parse(tokenMetadata)) does not contain null result e.t.c
    // you will also want to loop to return all the balances for all the token the address owns e.t.c
   /* .then((res) => {
        setBal(ethers.utils.formatUnits(res[0].balance, 18));
    }); */
  },[user]); 

This may not 100% work, but this is the idea you should be working to and it is going to get rid of most of the errors, if you still have troubles, you can post it here

1 Like

why are most of your usestates arrays (some may need to be, some may not) ?
and instead of using const use let :thinking:
and do some debugging and see what user.attributes.account returns
and you can try to remove the dependencies array or leave it empty to see if the error goes away ?

1 Like