You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
I'm using web3 1.0 beta 37 inside a simple javascript app in Chrome with Metamask 5.3.0 to try and obtain the account Metamask is connected to, which is a Ganache account behind the scenes. Here is the code:
if (window.ethereum) {
// Modern dapp browsers...
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.enable();
const accounts = await web3.eth.getAccounts();
App.account = accounts[0]; //This line crashes the script because accounts is undefined
} catch (error) {
// User denied account access...
console.error("Unable to retrieve your accounts! You have to approve this application on Metamask.");
}
} else if (window.web3) {
// Legacy dapp browsers...
window.web3 = new Web3(web3.currentProvider || "ws://localhost:8545");
App.displayAccountInfo();
return App.initContract();
} else {
// Non-dapp browsers...
console.log("Non-Ethereum browser detected. You should consider trying MetaMask!");
}
The line after web3.eth.getAccounts() crashes because accounts is undefined.
I don't know if it's Ganache's fault, Metamask's fault or a problem with web3.