0

I'm using Ganache to generate dummy accounts but every time I try to access the list of accounts using web3.eth.accounts or the web3.eth.getAccounts method, I only get access to the first account in the array. Can anyone advise on how I can get an array of all the accounts?

5
  • I can't reproduce this. I fired up ganache and ran new Web3(new Web3.providers.HttpProvider('http://localhost:8545')).eth.getAccounts((err, accounts) => { console.log(err, accounts); });, and it displayed multiple accounts.
    – user19510
    Commented Jan 15, 2018 at 16:56
  • Are you using Metamask? It seems like Metamask is picking the first account in the array. When I disable Metamask, I get all the accounts. How can I get all the accounts with Metamask enabled? Commented Jan 16, 2018 at 16:25
  • Ah, if you're connected to MetaMask, then you should get the list of accounts in MetaMask.
    – user19510
    Commented Jan 16, 2018 at 16:51
  • I'm pretty new to this, how do I get the accounts in MetaMask? Commented Jan 16, 2018 at 17:16
  • What you're doing should return the accounts in MetaMask.
    – user19510
    Commented Jan 16, 2018 at 17:17

1 Answer 1

1

For version 0.x of web3:

try this:

beforeEach(()=>{
web3.eth.getAccounts((err,acc)=>{
    if(err){
        console.log('error.............',err);
    }
    console.log(acc);
});
});

describe('contract',()=>{
it('deployes a contract',()=>{

});
});

or can simply try this :

 beforeEach(()=>{
 web3.eth.getAccounts(console.log);
 });

for version 1.x of web3:

beforeEach(()=>{
 web3.eth.getAccounts()
  .then(fetchedAccounts=>{
     console.log(fetchedAccounts);
  });
});

describe('Inbox',()=>{
it('deployes a contract',()=>{

  });
});

version 1.0.0 of web3 uses promises while 0.x doesn't work on promises it works on callbacks.

hope it helps!

Your Answer

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

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