Getting Unhandled Rejection (Error): [object Object]

I am trying to Fetch the users token from his wallet for which i am able to authenticate using the code below. Using logout() for logging out.

  const { Moralis,authenticate,isInitialized, isAuthenticated,isAuthenticating ,user, logout } = useMoralis();  
if (!isAuthenticated) {
    return (
            <Button boxShadow="xl" isLoading={isAuthenticating} onClick={() => authenticate()}>Connect Wallet</Button>
  }

But now when i want to fetch the user’s token balances i am getting a bug which says

Unhandled Rejection (Error): [object Object]

Code for fetching balances

const getBalance = async() => {
    const balances = await Moralis.Web3API.account.getTokenBalances();
    console.log("all token balances of a current user",JSON.stringify(balances));
  }

  useEffect(() => {
    if(isAuthenticated)
    {
      getBalance();
    }
  },[isAuthenticated]);

Hey @zimablue

Please share your server subdomain. There is a possible way how to fix it - visit the web3 api UI in admin panel https://admin.moralis.io/web3Api

Make a simple call and check again if it works correctly from the SDK

Are you asking for Server Url : https://ddnwackqf7dj.moralishost.com:2053/server
Yeah it works over there in the admin panel.
The api asks for the chain and the user address which i thought it will automatically take from my metamask wallet.
I guess I have to mention that also and do something like this.

    const options = { chain: 'eth', address: "0x..."}
const balances = await Moralis.Web3API.account.getTokenBalances(options);

So if am using authenticate to authenticate how will i use authenticate to get the user address? ```
const { Moralis,authenticate,isInitialized, isAuthenticated,isAuthenticating ,user, logout } = useMoralis();
if (!isAuthenticated) {
return (
<Button boxShadow=“xl” isLoading={isAuthenticating} onClick={() => authenticate()}>Connect Wallet
}

Cause if i am able to get the useraddress i could send the parameter as options and then make the api call and maybe if it will work.  Am i right?
1 Like

Hey @zimablue

You can use user.get("ethAddress") or user.attributes.ethAddress to recieve the ethAddress.

The api asks for the chain and the user address which i thought it will automatically take from my metamask wallet.

If you don’t specify the chain - it will automatically use eth

1 Like

Yeah thanks this works. Got my address but this api call is still not working. I am not getting any bug but this is returning an empty error. But in the api call this shows filled arrays.
I used

GET​/{address}​/erc20

const getBalance = async() => {
    const options = { chain: 'eth', loggedInAddress}
    const balances = await Moralis.Web3API.account.getTokenBalances(options);
    console.log("all token balances of a current user",balances);
  } 

And another thing I was’nt able to download api client sdk. I was trying trying to download typescript-fetch-client-generated but it fails. I have tried many times. The download doesn’t even start it just says server problem.

Try: const options = { chain: 'eth', address: loggedInAddress}

Yeah i was trying that only i did’nt mentioned it here properly.
But yeah still same thing. I have added my ropsten address over here which you can check it’s not empty. But still it’s showing empty

 const getBalance = async() => {
    const options = { chain: 'eth', address:"0x12aADAdd301d22c941DACF2cfa7A9e2019972F61"}
    const balances = await Moralis.Web3API.account.getTokenBalances(options);
    console.log("all token balances of a current user",balances);
  }

This works fine for me:

getBalance = async() => {
    const options = { chain: 'ropsten', address: "0x12aADAdd301d22c941DACF2cfa7A9e2019972F61"}
    const balances = await Moralis.Web3API.account.getTokenBalances(options);
    console.log("all token balances of a current user",balances);
  }

I am just doing an extra useEffect to call this. Other than that it’s still not working.
It’s showing

all token balances of a current user []

  useEffect(() => {
    if(loggedInAddress)
    {
      getBalance();
    }
  },[loggedInAddress]);

I tried this:

import { Moralis } from "moralis";

const appId = "APP_ID";
const serverUrl = "SERVER_URL";

Moralis.initialize(appId);
Moralis.serverURL = serverUrl;

const getBalance = async () => {
  const options = {
    chain: "ropsten",
    address: "0x12aADAdd301d22c941DACF2cfa7A9e2019972F61"
  };
  const balances = await Moralis.Web3API.account.getTokenBalances(options);
  console.log("all token balances of a current user", balances);
};

export default function App() {
  return (
    <div className="App">
      <button onClick={getBalance}> getBalance </button>
      <br />
    </div>
  );
}
1 Like

Yeah thanks this works. I was trying to use Moralis from useMoralis hook also without initializing the moralis appId and serverUrl. I just installed react-moralis and was using it.

const { authenticate,isAuthenticated,isAuthenticating ,logout ,Moralis} = useMoralis();  
1 Like