简体   繁体   English

Web3 1.1.0 的问题

[英]Issue with Web3 1.1.0

I am working on a React app which uses a library that has Web3 as a dependency.我正在开发一个 React 应用程序,它使用一个以 Web3 作为依赖项的库。 I had previously been able to the get the current Metamask address with the following code:我以前能够使用以下代码获取当前的 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.');
  }

Then I updated that library to its latest version which changed it's Web3 dependency to Web3 1.0 .然后我将该库更新到其最新版本,将其 Web3 依赖项更改为 Web3 1.0 。 Now when I run the exact same code I get the following error:现在,当我运行完全相同的代码时,出现以下错误:

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

Any thoughts as to what might be going on?关于可能发生什么的任何想法?

I've run into the same issue, i solved with this code:我遇到了同样的问题,我用这个代码解决了:

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 you need also to add ethereum.enable(); Makbe 你还需要添加ethereum.enable(); . . Hope this helps.希望这可以帮助。

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

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