18

truffle(development)> web3.eth.accounts[0]

gives output as "undefined"

truffle(development)> web3.eth.accounts

instead of showing all accounts details gives the below output

Accounts {

currentProvider: [Getter/Setter],


_requestManager:

RequestManager {

 provider:

  HttpProvider {
    host: 'http://127.0.0.1:7545',

    httpAgent: [Agent],
    timeout: 0,

    headers: undefined,

    connected: true,

    send: [Function],

   ......blah...blah

but...

truffle(development)> web3.eth.getAccounts

is working. please help

3
  • 2
    If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated. Commented Jan 27, 2019 at 8:23
  • I can use it but I am not able to choose any of the accounts specifically which I was able to do with 'web3.eth.accounts[0]" , please let me know the replacement for this? Commented Jan 27, 2019 at 9:06
  • 1. You cannot use it, as it is undefined; you even specify it in your own question! 2. You've already found a replacement, and again - you even specify it in your own question! Commented Jan 27, 2019 at 9:51

7 Answers 7

17

from @goodvibration in the comments :

web3.eth.accounts is deprecated.

That is true. in the newer version of web3.js web3.eth.getAccounts returns a promise, so that is why doing :

var accounts = web3.eth.getAccounts();
accounts[0];

will return undefined.

TLDR: use this code instead

web3.eth.getAccounts().then(function(acc){ accounts = acc })
accounts[0]
3
7

Use the code below to retrieve all the accounts

   var accounts = web3.eth.getAccounts();

Use this code to reference a single account

   var account0;
   web3.eth.getAccounts().then(function(result){
     account0 = result[0];
   })
1

One line code: For displaying all accounts use:

web3.eth.getAccounts()

This works perfectly for displaying individual account addresses: You can get the address of the account required by changing the index referenced by the result array in the fall back function

web3.eth.getAccounts().then( function (result) { return result[0] });

1

Also remember that MetaMask and other dapp browsers will stop populating the injected Ethereum provider with user accounts by default.

To call getAccountS() you need to use await ethereum.enable() first. Consider this example

const ethereum = window.ethereum;
const web3 = window.web3;
const web3Instance = new Web3(ethereum);
const enabledWeb3 = await ethereum.enable();
const account = await web3Instance.eth.getAccounts();
const accountAddress = await account[0];

This is just an example of what you should be doing now after November 2018. The legacy code will not display the account.

1
  • This helped me when using React. A pop-up appeared that said React wanted to connect to my account, and I clicked the button to allow it. Commented Nov 10, 2019 at 21:37
1

use this code :

const web=new Web3(window.ethereum.CurrentProvider || "http://localhost:8545")

0

If web3.eth.accounts is not showing the expected result then, follow these two simple step (Inside the truffle console)

  1. web3.eth.getAccounts().then(function(acc){ accounts = acc })

  2. accounts

That's is... if you want specific account then accounts[0/1/2...9]

0

All above solutions are correct with the fact that web3.eth.getAccounts() is returning a promise. But, Just make sure that you have to add await window.ethereum.enable() to actually connect your metamask wallet to the Dapp.

Because my code works after just adding it !!

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.