0

以前获取Metamask账号地址,突然无法获取账号,出现这个错误。

Uncaught (in promise) Error: No valid "from" address specified in neither the given options, nor the default options.

我这样指定地址

const accounts = await web3.eth.getAccounts()
    this.setState({ account: accounts[0] })

错误发生在此代码中

createPlace(name) {
    this.setState({ loading: true })
    this.state.placeList.methods.createPlace(name).send({ from: this.state.account })
    .once('receipt', (receipt) => {
      this.setState({ loading: false })
    })
  }

我的代码有问题吗?

请给我一些建议好吗?

4

1 回答 1

0

在这种情况下,Metamask 无法连接到浏览器并将 web3 注入其中。

一种解决方案可以是您的代码中可能有一行检查CurrrentProvider吗?

前任:

if (typeof window !== 'undefined' && typeof window.web3 !== 'undefined') {
  // We are in the browser and metamask is running.
  web3 = new Web3(window.web3.currentProvider);
} 

将行更改为

window.web3.currentProvider.enable()

并在保持服务器运行的同时保存文件。

这应该适合你。

您可以做的另一件事是在 try 块中编写逻辑代码并执行

window.web3.currentProvider.enable()
于 2019-04-25T07:10:12.253 回答