[SOLVED] Token balance not showing

I would like to know if the token balance only showing if its not 0 value
because i have some token on my metamask only eth have some amount other token are 0 value.

can i also ask some help how to resolve this issue
index.html:60 Uncaught (in promise) ReferenceError: tokenValue is not defined
at index.html:60
at Array.map ()
at getStats (index.html:57)

and some time this error are showing when i refresh my browser
moralis.js:29067 POST https://hvvgp61mmee7.usemoralis.com:2053/server/functions/getTokenBalances 400

    index.html:65 Uncaught (in promise) {code: 141, error: 'required param address not provided'}

moralis code

 const serverUrl = "";
        const appId = "";
        Moralis.start({ serverUrl, appId });

        // Log in with Metamask
        async function login() {
            let user = Moralis.User.current();
            if (!user) {
                user = await Moralis.authenticate();
            }
            console.log("logged in user:", user);
            getStats();
        }


        // getstats
        async function getStats() {
            const balances = await Moralis.Web3API.account.getTokenBalances({chain: 'bsc'});
            console.log(balances);
          
        }


        // Log out
        async function logOut() {
            await Moralis.User.logOut();
            console.log("logged out");
        }

        // bind button click handlers
        document.getElementById("btn-login").onclick = login;
        document.getElementById("btn-logout").onclick = logOut;

try to add await Moralis.enableWeb3() before that line, so that it can get current ETH Address that is selected in MetaMask.

i just want to confirm
so its only showing balance with value > 0 l?
and it will not show balance with 0 value?

and is this correct
await Moralis.enableWeb3();
const balances = await Moralis.Web3API.account.getTokenBalances({chain: ‘bsc’});

because i still see this issue
index.html:61 Uncaught (in promise) ReferenceError: tokenValue is not defined

  • at index.html:61*
  • at Array.map ()*
  • at getStats (index.html:58)*

I don’t know where from you have that tokenValue error, in the pasted code I don’t see any tokenValue variable.
I don’t know about the first question, it depends on the code, getTokenBalances should return only tokens with a balance: https://docs.moralis.io/moralis-server/web3-sdk/account#gettokenbalances

here’s the complete code

const serverUrl = "";
        const appId = "";
        Moralis.start({ serverUrl, appId });

        const $tokenBalanceTbody = document.querySelector('.js-token-balances');

        // Log in with Metamask
        async function login() {
            let user = Moralis.User.current();
            if (!user) {
                user = await Moralis.authenticate();
            }
            console.log("logged in user:", user);
            getStats();
        }

        // getstats
        async function getStats() {
            await Moralis.enableWeb3();
            const balances = await Moralis.Web3API.account.getTokenBalances({chain: 'bsc'});
            console.log(balances);
            $tokenBalanceTbody.innerHTML = balances.map( (token, index) => `
                <tr>
                    <td>${index + 1}</td>
                    <td>${token.symbol}</td>
                    <td>${tokenValue(token.balance, token.decimals)}</td>
                    <td>button</td>
                </tr>
            `);
        }

        // Log out
        async function logOut() {
            await Moralis.User.logOut();
            console.log("logged out");
        }

        // bind button click handlers
        document.getElementById("btn-login").onclick = login;
        document.getElementById("btn-logout").onclick = logOut;

here’s the result screenshot

it looks like it tries to call a function named tokenValue here.

i just follow the tutorial
actually i even try the metamask login but its not working
so i use my old code so i can log on metamask

btw about the tutorial on dex even i follow the video and code
the results are different from my end

just another question about the academy course
are they all updated?

where from is that code, what is the exact tutorial that you mention?

2021 JavaScript Programming for Blockchain Developers

Building a Dex

about the video tutorial
do they also have github file
so we can compare the code

can you point me to that GitHub code?

i don’t have yet a github file

ok, that was a question, I didn’t read it right

btw its ok now
i forgot to add this code

//Converting from Wei using custom function
        const tokenValue = (value, decimals) =>
            (decimals ? value / Math.pow(10, decimals) : value);

output
|#|Symbol |Amount |Action|
|1|ETH |0.000943 |button|

thank you for the help again

1 Like