[SOLVED] web3API: Cannot read properties of undefined (reading 'account')

Hi, I am trying to fetch balance of an address from the frontend, but it keep showing error:

const fetchBotBalance = async (network, address) => {

    await Moralis.initialize(secret.bsc_testnet.appId);

    Moralis.serverUrl = secret.bsc_testnet.serverUrl;

    const options = { chain: network, address: address }; //, to_block: "10253391" };

    const balances = await Moralis.Web3API.account.getTokenBalances(options);
//Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'account') 


    console.log("GOt All balances = ", balances);

    return balances;

};

Could some one please help?

Hey @tungtien

Update your SDK version :raised_hands:

I have updated server to 0.0.273. But it doesn’t help.
:sob:

I also ran:

npm i moralis

but doesn’t help

setTimeout(2000) before you return stopped the error for me, I literally just dealt with the same thing… only thing the balance returned null

you can check Moralis SDK version with console.log(Moralis.CoreManager.get("VERSION"))

I dont think your solution will works as the errors shows that Web3API is missing.


const fetchBotBalance = async (network, address) => {

    await Moralis.initialize(secret.bsc_testnet.appId);

    Moralis.serverUrl = secret.bsc_testnet.serverUrl;

    user = Moralis.User.current();

    console.log("user = ", user);  
// user logged out properly

    const options = { chain: network, address: address }; //, to_block: "10253391" };

    const balances = await Moralis.Web3API.account.getTokenBalances(options);
// Error: ..Cannot read properties of undefined (reading 'account') 

    console.log("GOt All balances = ", balances);

    return balances;

};

what error in particular you had?

where do you run that? I got an error in node

Pls read error in comments in the code

On javascript Front end

This error: //Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'account') ?

1 Like

this works fine for me:

fetchBotBalance = async (network, address) => {

    options = { chain: network, address: address }; //, to_block: "10253391" };

    balances = await Moralis.Web3API.account.getTokenBalances(options);
    console.log("GOt All balances = ", balances);

    return balances;
};
1 Like

To be precise, I had this code inside a function called by useEffect( async ()=>{....}) from inside React js app

the same exact error

In react you have

import { useMoralisWeb3Api } from "react-moralis";
  const Web3Api = useMoralisWeb3Api();
  await Web3Api.account.getTokenBalances(
1 Like

yeah, this is what I maybe missing…
thanks. I will try that…

Attempted import error: ‘useMoralisWeb3Api’ is not exported from ‘react-moralis’.

I will try reinstall react-moralis for npm…

maybe u forgot the brackets {}