繁体   English   中英

Web3 1.1.0 的问题

[英]Issue with Web3 1.1.0

我正在开发一个 React 应用程序,它使用一个以 Web3 作为依赖项的库。 我以前能够使用以下代码获取当前的 Metamask 地址:

 const injectedWeb3 = window.web3 || undefined;

 this.state = {
      web3: injectedWeb3
    };

  getAccount() {
    const { web3 } = this.state;
    if (web3.eth.accounts[0]) return web3.eth.accounts[0];
    throw new Error('Your MetaMask is locked. Unlock it to continue.');
  }

然后我将该库更新到其最新版本,将其 Web3 依赖项更改为 Web3 1.0 。 现在,当我运行完全相同的代码时,出现以下错误:

Error: Invalid JSON RPC response: undefined 
TypeError: e is not a function[Learn More] 

关于可能发生什么的任何想法?

我遇到了同样的问题,我用这个代码解决了:

web3.eth.getAccounts(function (err, accounts) {
      if (err != null) {
        console.log(err)
      }
      else if (accounts.length === 0) {
        console.log('MetaMask is locked');
      }
      else {
        console.log('MetaMask is unlocked'); 
        console.log(accounts[0]);
      }
    });

Makbe 你还需要添加ethereum.enable(); . 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM