Ethereum 如何从Metamask获取帐户地址?

Ethereum 如何从Metamask获取帐户地址?,ethereum,solidity,web3,Ethereum,Solidity,Web3,accounts[0]返回此代码中未定义的 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) 但是,此代码更适合

accounts[0]
返回此代码中未定义的

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)
    })

web3.eth.accounts[0]
对我来说很好,请确保登录到metamask帐户