5

The command:

web3.eth.accounts

Returns an empty array despite Metamask having one account, how should I be retrieving the accounts in a way that will work with Mist and Metamask?

5

Turns out for Metamask to do or return anything you need to access the dapp through a local or remote webserver and not through the local filesystem (file://) due to browser security restrictions.

3

The safest way to do it is to call asynchronously, web3.eth.getAccounts(function(err, accounts) { }).

You will also get an empty array if the user is not signed in.

  • 3
    web3.eth.getAccounts(function(err, accounts) { console.log(accounts); }) – Samuel Barnes Jun 27 '17 at 10:53
  • Using breakpoints I have deduced that execution never reaches the code within the callback. Any ideas? – Samuel Barnes Jun 27 '17 at 10:55
  • Alternate simple syntax: web3.eth.getAccounts(console.log). If the code hasn't reached that block, it makes me wonder if the page's connection was broken. Reload the page and try again? – DanF Jun 27 '17 at 15:58
  • Turns out for Metamask to do or return anything you need to access the dapp through a local or remote webserver and not through the local filesystem (file://) due to browser security restrictions. – Samuel Barnes Jun 27 '17 at 16:31
  • How can I see the difference between "account exists and is locked" and "no account exists" ? I need to know this to ask the user want to create an account with mascara. – digitaldonkey Apr 13 '18 at 17:52
2

I'm also facing same problem. My ganache was running, metamask is also running, web3 is also injected. The problem was web3.eth.accounts and web3.eth.getAccounts().then(...) was returning null. But after lot of searching I found that

ethereum.enable()

I run this command and one metamask popup was opened, which connected my metamask account to app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.