Metamask-extension: web3.eth.accounts[0] always comes as undefined.

Created on 9 Mar 2018  路  7Comments  路  Source: MetaMask/metamask-extension

my condition (typeof web3 !== 'undefined') is true
inside this condition i write
window.web3 = new Web3(web3.currentProvider);
but alway web3.eth.accounts[0]; is coming as 'undefined' I even called it via web3.eth.getAccounts() but in vain .
I checked my Metamask it is working fine as i tried to submit some contract via remix to ropstan and it worked without any problem.

Any Help . Please find below as my code snippet.
` window.addEventListener('load', () => {
console.log('codepen load' + web3.eth.accounts);
if (typeof web3 !== 'undefined') {
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
//window.web3 = new Web3(Eth.givenProvider || 'ws://some.local-or-remote.node:8545');

   // web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8545');
    alert('Metamask used');
}
 else {
    console.log('No web3? You should consider trying MetaMask!')
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/token"));
}

setTimeout(function()
{

    console.log('codepen timeout ' + web3.eth.accounts[0]);
    alert('after timeout');
var res  = web3.eth.getAccounts()
 {                   
    console.log(res);
    web3.eth.defaultAccount = web3.eth.accounts[0];
    alert('Sandeep'); 
    console.log(web3.eth.accounts[0]);

}

}, 2000)
alert( 'inside timeout');

});`

Most helpful comment

You might be using web3js 1.0 beta which is not fully supported by MetaMask based on this open issue

To make sure try

const web3 = new Web3(window.web3.currentProvider);
console.log(web3.eth.accounts)

If you're using 1.0 it will print an object described in 1.0. documentation

As a hacky workaround (if you really want to use web3js 1.0. beta) you could save the defaultAccount before discarding the web3 provided by MetaMask.

const defaultAccount = web3.eth.defaultAccount
const web3 = new Web3(window.web3.currentProvider);

All 7 comments

$ testRPC
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
it show all Available Accounts and Private keys.

hey @sandeep1116 are you seeing any errors in the console from metamask also is your metamask unlocked?

if your metamask is unlocked could you check you background logs (instructions on how to do that here) and also check to see if web3.currentProvider.publicConfigStore.getState() returns an object that has the selectedAddress set

You might be using web3js 1.0 beta which is not fully supported by MetaMask based on this open issue

To make sure try

const web3 = new Web3(window.web3.currentProvider);
console.log(web3.eth.accounts)

If you're using 1.0 it will print an object described in 1.0. documentation

As a hacky workaround (if you really want to use web3js 1.0. beta) you could save the defaultAccount before discarding the web3 provided by MetaMask.

const defaultAccount = web3.eth.defaultAccount
const web3 = new Web3(window.web3.currentProvider);

I use Metamask with web3 1.0.0, to get the current account I call:
web3.eth.getAccounts(), this gives an array which has maximum one element, I mean it has the unlocked account or it is empty.

@sandeep1116 it looks like there's some good comments here鈥擨'm going to close this for now, if you run into more issues feel free to open up a new issue.

hey guys i am using web3 1.2.1. Its always says

Cannot read property 'currentProvider' of undefines

const Web3 =  require('web3')  
const window = require('window');
const web3 = new Web3(window.Web3.currentProvider);

the comments seem to be familiar with my approach that's why reopening the thread.

Hey @parin13 - that might be because you've capitalized web3. The web3 property on window is lower-cased.

Was this page helpful?
0 / 5 - 0 ratings