How to use onAccountsChanged?

I would like to extend this basic javascript boilerplate code with the accountChanged function

const unsubscribe = Moralis.onAccountsChanged()
console.log(unsubscribe);

here is a the code

/** Connect to Moralis server */
const serverUrl =
const appId =
Moralis.start({ serverUrl, appId });

/** Add from here down *
async function login() {
let user = Moralis.User.current();
if (!user) {
try {
user = await Moralis.authenticate({ signingMessage: “Hello World!” })
console.log(user)
console.log(user.get(‘ethAddress’))
} catch (error) {
console.log(error)
}
}
}

const unsubscribe = Moralis.onAccountsChanged(function(accounts) {
console.log(account);
});
async function logOut() {
await Moralis.User.logOut();
console.log(“logged out”);
}

document.getElementById(“btn-login”).onclick = login;
document.getElementById(“btn-logout”).onclick = logOut;

It looks like you used accounts as parameter name in one place and account on console log

actually it was working when fixing the typo,
however, user.get(‘ethAddress’) gives me just the address, after accounts changed function, i get back an array, like this

0x1234AbcdEF45678
Array [ “0x3457980565ASDF6tgHBU” ]

I just tested this:

Moralis.onAccountsChanged(function(account) {
console.log(account);
});

and works fine for me when I change the account in MetaMask

check above reply please
because when i get back an array, this messes up my code
so isnt there a better way on using this onAccountsChanged?

this is not related to changing accounts, this is related to current user address that was user for authentication, the use address will remain the same even if the account is changed in metamask

after changing accounts, it returns an array in the console.log(accounts)
and i cant use that

Whats the proper way of using this function?

When the user chages account, obviously everything else changes to, like balance etc.
but i cant work with that when the first time a user logs in i get back the user account like a regular address and than it shows up as an array
so how do i fix this so my code works like on every other application

ok, it returns an array, what is the problem with returning an array?

i cant use the array, i need the the output to be the address only not with all that array[‘0x123456jdgjk’]

you could use something like this:

Moralis.onAccountsChanged(function(accounts) {
console.log(accounts[0]);
});

All I try to do is this


 async function login(provider) {
    let user = Moralis.User.current();
    if (!user) {
        try {
            user = await Moralis.authenticate({ signingMessage: "Just sign this shit", provider })
            $("#myModal").modal("hide")
            console.log(user.get('ethAddress'))
            account = user.get('ethAddress')

            // get BSC native balance for a given address
            const options = { chain: "bsc", address: account};
            const balance = await Moralis.Web3API.account.getNativeBalance(options);
            console.log(balance)
                        
        } catch (error) {
            console.log(error)
        }
    }
  }
  Moralis.onAccountsChanged(function(accounts) {
   console.log(accounts);
    });

All I want is to get the native balance of the current logged in metamask account and it doesnt work with on accounts changed, after changing the account, i dont get the balance of the new account

I don’t see code there to display the balance when the account is changed in metamask.
I think that you need to add code here:

because i dont know, what to write
I also dont know why this function comes with a default of an account function, like why is it even here?

thatswhy im asking what is the proper way to use this function

you add this code there

please post the code of the whole function
cause the onAccounts function isnt even an async function

 Moralis.onAccountsChanged(function(accounts) {
    const options = { chain: "bsc", address: account[0]};
    const balance = await Moralis.Web3API.account.getNativeBalance(options);
    console.log(balance)
    });

And please explain, why is this ```
Moralis.onAccountsChanged(function(accounts)

function accounts right there, why do i need this?

why do you need what in particular?

you add a callback function to onAccountsChanged that will be called when that event happens
you can call any other function in that callback, you can make it async if you want

can you not just post the code?

All i want to do is when a user logs in, i want to show the balance and when he changes the account, it should show the different balance from his other account

So can you please post it?

you have all the information available to do that
what problems do you have in writing that code?

why dont you just post the code?

I need this code to be fixed
especially the onAccountsChanged function

 /** Connect to Moralis server */
 const serverUrl = 
 const appId =
 Moralis.start({ serverUrl, appId });

 /** Add from here down */
 async function login(provider) {
    let user = Moralis.User.current();
    if (!user) {
        try {
            user = await Moralis.authenticate({ signingMessage: "Just sign this shit", provider })
            $("#myModal").modal("hide")
            console.log(user.get('ethAddress'))
            account = user.get('ethAddress')

            // get BSC native balance for a given address
            const options = { chain: "bsc", address: account};
            const balance = await Moralis.Web3API.account.getNativeBalance(options);
            console.log(balance)
                        
        } catch (error) {
            console.log(error)
        }
    }
  }
  Moralis.onAccountsChanged() {
    const options = { chain: "bsc", address: account};
    const balance = await Moralis.Web3API.account.getNativeBalance(options);
    console.log(balance)
    });

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

 
 document.getElementById("btn-login-metamask").onclick = login;
 document.getElementById("btn-login-wc").onclick = login("walletconnect");
 document.getElementById("btn-logout").onclick = logOut;