Javascript 获取web3.eth.accounts(元掩码)时出现问题

Javascript 获取web3.eth.accounts(元掩码)时出现问题,javascript,ethereum,web3js,web3,metamask,Javascript,Ethereum,Web3js,Web3,Metamask,我的元掩码已启用,但在控制台中我看到空数组,当我尝试console.log(web3.eth.accounts[0])时,它返回undefined 但当我使用console.log(web3.eth)时,我会在控制台中看到所有数据 有人知道为什么web3.eth.accounts[0]或web3.eth.accounts不起作用吗 试验 addEventListener('load',function()){ 如果(web3的类型!=“未定义”){ console.log(“检测到web3”

我的元掩码已启用,但在控制台中我看到空数组,当我尝试
console.log(web3.eth.accounts[0])
时,它返回
undefined

但当我使用
console.log(web3.eth)
时,我会在控制台中看到所有数据

有人知道为什么
web3.eth.accounts[0]
web3.eth.accounts
不起作用吗


试验
addEventListener('load',function()){
如果(web3的类型!=“未定义”){
console.log(“检测到web3”);
web3=新的web3(web3.currentProvider);
日志(web3.eth.accounts);
}否则{
警报(“请安装MetaMask”);
}
});

我使用以下方法解决了问题:

 web3.eth.getAccounts((err, res) => {                   
                   console.log(res[0]);
});

但无论如何,我还是要问,为什么web3.eth.accounts[0]不起作用?

web3.eth.accounts[0]
不起作用,因为
web3.eth.accounts
并不是一个帐户列表,正如它在

这样获得帐户:

var account = null;
web3.eth.getAccounts(async function(error, accounts) {
  if (error == null && accounts.length > 0) {
      account = accounts[0];
   }
}

它不起作用,因为它不是您期望的帐户列表。它是一个可以使用的整个包的构造函数。查看更多详细信息