gnxvw
gnxvw

Reputation: 454

How to get an account address from Metamask?

accounts[0] returns undefined in this code.

const accounts = await web3.eth.getAccounts()
console.log(accounts[0])

I uninstalled Metamask and reset the account, but that didn't work.

web3 version is [email protected].

Could you give me any advise, why I cannot get an account address from Metamask?

Upvotes: 4

Views: 8360

Answers (1)

gnxvw
gnxvw

Reputation: 454

There was a Metamask connection error. I connected to web3 by coding this.

Web3(window.web3.currentProvider)

However, this code is better for modern wallet.

web3 = new Web3(window.ethereum)
    window.ethereum.enable().catch(error => {
        // User denied account access
        console.log(error)
    })

Upvotes: 5

Related Questions