Undefined results for “web3.eth.accounts” in truffle console












2















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










share|improve this question


















  • 1





    If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

    – goodvibration
    Jan 27 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?

    – Yash Shukla
    Jan 27 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!

    – goodvibration
    Jan 27 at 9:51
















2















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










share|improve this question


















  • 1





    If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

    – goodvibration
    Jan 27 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?

    – Yash Shukla
    Jan 27 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!

    – goodvibration
    Jan 27 at 9:51














2












2








2








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










share|improve this question














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







web3js truffle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 27 at 7:24









Yash ShuklaYash Shukla

235




235








  • 1





    If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

    – goodvibration
    Jan 27 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?

    – Yash Shukla
    Jan 27 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!

    – goodvibration
    Jan 27 at 9:51














  • 1





    If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

    – goodvibration
    Jan 27 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?

    – Yash Shukla
    Jan 27 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!

    – goodvibration
    Jan 27 at 9:51








1




1





If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

– goodvibration
Jan 27 at 8:23







If web3.eth.getAccounts is working, then why don't you simply use it??? As of web3.js v1, web3.eth.accounts is deprecated.

– goodvibration
Jan 27 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?

– Yash Shukla
Jan 27 at 9:06





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?

– Yash Shukla
Jan 27 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!

– goodvibration
Jan 27 at 9:51





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!

– goodvibration
Jan 27 at 9:51










2 Answers
2






active

oldest

votes


















3














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 (if it works, didn't test) :



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





share|improve this answer































    2














    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];
    })





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "642"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f66202%2fundefined-results-for-web3-eth-accounts-in-truffle-console%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      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 (if it works, didn't test) :



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





      share|improve this answer




























        3














        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 (if it works, didn't test) :



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





        share|improve this answer


























          3












          3








          3







          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 (if it works, didn't test) :



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





          share|improve this answer













          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 (if it works, didn't test) :



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 27 at 9:31









          Kaki Master Of TimeKaki Master Of Time

          1,261319




          1,261319























              2














              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];
              })





              share|improve this answer




























                2














                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];
                })





                share|improve this answer


























                  2












                  2








                  2







                  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];
                  })





                  share|improve this answer













                  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];
                  })






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 27 at 10:10









                  Alex WAlex W

                  391




                  391






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ethereum Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f66202%2fundefined-results-for-web3-eth-accounts-in-truffle-console%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory