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?

4 Answers 4

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.

6
  • 3
    web3.eth.getAccounts(function(err, accounts) { console.log(accounts); }) Jun 27, 2017 at 10:53
  • Using breakpoints I have deduced that execution never reaches the code within the callback. Any ideas? Jun 27, 2017 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, 2017 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. Jun 27, 2017 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. Apr 13, 2018 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.

0
  1. Login in MetaMask
  2. Open settings in MetaMask
  3. Go to Connections and add localhost in Add Site and click Connect(By doing this you are giving permission to localhost to get access to your MetaMask Account)
  4. Now you can get the MetaMask account in your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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