[SOLVED] React & moralis can't get user token balances (throws error with code 141)

Hello,
I am using react and moralis for my project and I want to get token balances for the current user. Wrote code by documentation, but something does not work.

My code:

const Web3Api = useMoralisWeb3Api();
const { fetch, data, error, isLoading } = useMoralisWeb3ApiCall(
    Web3Api.account.getTokenBalances
);

useEffect(() => {
    fetchBalance();
}, []);

const fetchBalance = () => {
    fetch();

    if (data) {
        console.log(data);
    } else if (error) {
        console.log(error);
    }
};

It throws error:
{code: 141, error: "[object Object]"}

react-moralis documentation:

What is wrong with my code and what does this error mean?
Thanks

1 Like

Hi,
Try first with the example from the documentation to see if it works, there it calls getBlock function with a parameter.
In your case with Web3Api.account.getTokenBalances you may have to pass it a parameter if the user is not logged in already.

1 Like

I figured it out. I was using the old moralis package version. After I have updated it everything works perfectly.

1 Like

Hello, I have additional question for token balance using React and Moralis. Why Moralis can get “kovan” balance, but not “bsc”?
My code:

const fetchBalance = async () => {
    let balance = await Web3Api.account.getTokenBalances({
        chain: "bsc",
    });

    const balancesObject = balance.reduce((previousObject, currentItem) => {
        previousObject[currentItem.token_address] =
            fromIntegerStringToDecimalString(
                currentItem.balance,
                currentItem.decimals
            );

        return previousObject;
    }, {});

    console.log("bsc -> ", balancesObject);
    setBalances(balancesObject);
};

console log with bsc:
image

with kovan:

MetaMask balance:
image

I don’t understand why it can’t fetch bsc balance. What should I do? Thanks.

Hi,

I just tried fetching on the bsc mainnet and was able to get all the tokens.

This is my code –

import { useMoralisWeb3ApiCall, useMoralisWeb3Api } from "react-moralis";
 const Web3Api = useMoralisWeb3Api();

const {
    fetch,
    data,
    error,
    isLoading,
  } = useMoralisWeb3ApiCall(Web3Api.account.getTokenBalances, {
    chain: "bsc",
  });

const balances = fetch();

This is the new hook syntax in the react-moralis package. You can update to see it in action.

1 Like

I updated the react-moralis package and implemented your provided code, but it still doesn’t give me BNB balance.

image

As earlier, it works with Ethereum Kovan Testnet. I don’t understand why it works with one network, but not with another?

Is iit possible to share the address of which you’re trying to retrieve?

You don’t get the BNB balance with Web3Api.account.getTokenBalances, you’ll have to use getNativeBalance (https://docs.moralis.io/moralis-server/web3-sdk/account#getnativebalance). That is because BNB is the native currency and not an ERC20 token.

2 Likes

Thank you, it works.