Problem to display the balance of a wallet

Hello everyone,

The newbie that I am still has a little problem so I allow myself to ask for a little help.
I would like to display the BNB balance of a wallet on my website.
The code is simple and short, when I open the console of my browser the balance is displayed but on the site page I have [object Object].

Here are the details:

Html:

<div class="balance1">
<div id="balance">
  <p>Balance:</p>
  <div id="balance"> BNB</div>
  </div>
  </div>
  <script type="text/javascript" src="./main1.js"></script>

main1.js:

Moralis.initialize("5JNM9X1Nq8ciDngst2pNBUrSoThG5a7jBPYUvrZM"); // Application id from moralis.io
Moralis.serverURL = "https://xsmb9wqekxnl.moralishost.com:2053/server"; //Server url from moralis.io

async function getBalance() {
	balance = await Moralis.Web3API.account.getNativeBalance({chain: "bsc", address: "0x67422A4b99D5a22094e54D4e7Ba2Da36431d5C50"});
    console.log(balance)
    let ethBalance = balance.ethBalance + " BNB"
    document.getElementById("balance").innerHTML = balance;
}
getBalance();

Console:

Screenshot_23

When I click on the โ€œXHR finishedโ€ link of the console I have this:
{โ€œcodeโ€: 141, โ€œerrorโ€: โ€œ[object Object]โ€}

Thanks in advance for the help

try to replace those 2 lines with:

 let ethBalance = balance.balance + " BNB"
    document.getElementById("balance").innerHTML = ethBalance;
1 Like

Great thank you very much, now I have the amount in BNB but it is a whole number without decimal.
I tried that to get the comma but to no avail.

async function getBalance() {
	balance = await Moralis.Web3API.account.getNativeBalance({chain: "bsc", address: "0x67422A4b99D5a22094e54D4e7Ba2Da36431d5C50"});
    console.log(balance)
    let ethPrice = (price.nativePrice.value / (18**price.nativePrice.decimals)) + " BNB"
    console.log(ethPrice)
    let ethBalance = balance.balance + " BNB"
    document.getElementById("balance").innerHTML = ethBalance;
}
getBalance();

you could try to use this: https://docs.moralis.io/moralis-server/sdk-utils/moralis-units#converting-token-value-from-wei

I have done a lot of testing but not working.
Thanks anyway I will try to find another solution.