![](/img/trans.png)
[英]How to get the public and private keys of an account from Metamask | ReactJS
[英]Cannot get metamask account
我曾经获得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 })
})
}
我的代码有问题吗?
你能给我任何建议吗?
在这种情况下,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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.