[英]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.