Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

getAccounts returns undefined #2196

@sarbogast

Description

@sarbogast

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions