Web3.js: web3.eth.getAccounts() returns empty array

Created on 2 Dec 2018  路  8Comments  路  Source: ChainSafe/web3.js

Hi,
I'm using MetaMask and Ropsten provider.

Trying to get list of accounts by this code:

var accounts = web3.eth.getAccounts((error,result) => {
        if (error) {
            console.log(error);
        } else {
            console.log(result);
        }       
    });

, but always result is empty array.

My metamask is enable and logged in.
Web3 version: 1.0.0.

code before getting accounts:

var Web3 = require('web3');

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/<token>'));
}

Please tell me, what I'm doing wrong ?

support

Most helpful comment

Hey thanks for the issue, MM have released a breaking change on November the 6th which means people had to approve that web3 should be injected into the browser

https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

Looks like you are doing it the old way, if you do not do it the new way web3.eth.getAccounts will always return a empty array as its not been authenticated.

Hope the article helps, let me know 馃憤

All 8 comments

Hey thanks for the issue, MM have released a breaking change on November the 6th which means people had to approve that web3 should be injected into the browser

https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

Looks like you are doing it the old way, if you do not do it the new way web3.eth.getAccounts will always return a empty array as its not been authenticated.

Hope the article helps, let me know 馃憤

Thanks @joshstevens19 ! It helped me.
Also, I found out that MM doesn't work with local html file. I mean, if I'm trying testing in browser with local path, such as "file:///D:/project/index.html", MetaMask doesn't inject the variable "window.ethereum" and I can't set MetaMask provider and get current accounts.
After installing the Chrome application "Web Server for Chrome" and setting localhost path, like a "http://127.0.0.1:8887/project/index.html", everything works great!

great glad to help! Thanks

Add your site in metamask >settings>Connections>add site

Added localhost and 127.0.0.1 in metamask but still returns empty array.
Unless https is required, I don't know what it is.

This problem is because the dapps and metamask have stopped exposing user accounts by default on web page.
Just use 'ethereum.enable()' as first line of your javascript code.
It worked for me.

Alternate Solution
Step1: Visit the website from which you want to access MetaMask .
Step2: Click on the account option button(vertical ellipsis icon) of MetaMask extension.
Step3: Click on the Connected sites menu.
Step4: Click on "_Manually connect to current site_" and then grant all the permission that it asks for.

You will get a _connected status_ at the top left corner.

This problem is because the dapps and metamask have stopped exposing user accounts by default on web page.
Just use 'ethereum.enable()' as first line of your javascript code.
It worked for me.

This works perfectly. Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zamoore picture zamoore  路  3Comments

ragnu picture ragnu  路  3Comments

SCBuergel picture SCBuergel  路  3Comments

gabmontes picture gabmontes  路  3Comments

xpepermint picture xpepermint  路  3Comments