如何从 Metamask 获取账户地址?

How to get an account address from Metamask?

accounts[0] returns undefined 在此代码中。

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

我卸载了 Metamask 并重置了帐户,但这没有用。

web3 版本为 web3@1.0.0-beta.37.

你能给我一些建议吗,为什么我无法从 Metamask 获得帐户地址?

存在 Metamask 连接错误。 我通过编码连接到 web3。

Web3(window.web3.currentProvider)

但是,此代码更适合现代钱包。

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