'How to get BNB token balance in metamask wallet in React?

This is snippet of my code.

    const getBalance = (userAddress: String) => {
    const provider = new Web3((window as any).web3.currentProvider);
    const bnbContract = new provider.eth.Contract(bnbTokenAbi, tokenAddress);
    bnbContract.methods.balanceOf(userAddress).call().then((res: any) => {
      setBalance(res);
    }).catch((err: any) => {
      console.log(err);
    });
  }

But only the catch function works, what's the reason?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source