Accessing accounts in metamask through web3.js 1.xBetter pattern to detect web3 default account when using metamaskHow to detect MetaMask loginIssue with getting web3.eth.accounts (MetaMask)Using web3 from MetaMask in ReactHow to get reference to Metamask account from Node.jsmetamask web3 is undefinedProblems with metamask/web3 connectionHow to handle user login in Ethereum DApps?Use local Metamask account with Infura nodeUse Web3 and Metamask in React

Are those flyers about apartment purchase a scam?

Why is tert-butoxide often used in elimination reactions when it is not necessary?

What unique challenges/limitations will I face if I start a career as a pilot at 45 years old?

How would armour (and combat) change if the fighter didn't need to actually wear it?

How do I call a 6-digit Australian phone number with a US-based mobile phone?

Why command hierarchy, if the chain of command is standing next to each other?

"Table of Astronomy's" depiction of the solar system models

Do I have to cite common CS algorithms?

Shifting tenses in the middle of narration

If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?

Scam? Phone call from "Department of Social Security" asking me to call back

(A room / an office) where an artist works

Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

How far did Gandalf and the Balrog drop from the bridge in Moria?

What is a "soap"?

Causal Diagrams using Wolfram?

Why did IBM make the PC BIOS source code public?

Help, I cannot decide when to start the story

Can lodestones be used to magnetize crude iron weapons?

An array battle with odd secret powers

How did Arecibo detect methane lakes on Titan, and image Saturn's rings?

Word for an event that will likely never happen again

What is the status of this patent?

How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?



Accessing accounts in metamask through web3.js 1.x


Better pattern to detect web3 default account when using metamaskHow to detect MetaMask loginIssue with getting web3.eth.accounts (MetaMask)Using web3 from MetaMask in ReactHow to get reference to Metamask account from Node.jsmetamask web3 is undefinedProblems with metamask/web3 connectionHow to handle user login in Ethereum DApps?Use local Metamask account with Infura nodeUse Web3 and Metamask in React






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.



I'm importing the library (beta.37) via:
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"



To make things more complicated:
1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
2) in Chrome (same build) it would always returns undefined.



To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.



Consequently, I can't use the following function, to dynamically print the current address:



var accountInterval = setInterval(function() 
if (web3.eth.accounts[0] !== userAccount)
userAccount = web3.eth.accounts[0];

, 100);









share|improve this question
































    0















    I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.



    I'm importing the library (beta.37) via:
    <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"



    To make things more complicated:
    1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
    2) in Chrome (same build) it would always returns undefined.



    To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.



    Consequently, I can't use the following function, to dynamically print the current address:



    var accountInterval = setInterval(function() 
    if (web3.eth.accounts[0] !== userAccount)
    userAccount = web3.eth.accounts[0];

    , 100);









    share|improve this question




























      0












      0








      0








      I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.



      I'm importing the library (beta.37) via:
      <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"



      To make things more complicated:
      1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
      2) in Chrome (same build) it would always returns undefined.



      To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.



      Consequently, I can't use the following function, to dynamically print the current address:



      var accountInterval = setInterval(function() 
      if (web3.eth.accounts[0] !== userAccount)
      userAccount = web3.eth.accounts[0];

      , 100);









      share|improve this question
















      I want to retrieve the currently selected account in my metamask plugin through web3.js. And I want to do it dynamically, so when switched to another account, it should be printed to the UI.



      I'm importing the library (beta.37) via:
      <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.37/dist/web3.min.js"></script>"



      To make things more complicated:
      1) In Brave web3.eth.accounts[0] would log my current address in any other site than my dApp, yet here, it returns "undefined".
      2) in Chrome (same build) it would always returns undefined.



      To me it is inexplicable how it can return undefined, when other dApps that are built on web3.js 0.x use that exact same code.



      Consequently, I can't use the following function, to dynamically print the current address:



      var accountInterval = setInterval(function() 
      if (web3.eth.accounts[0] !== userAccount)
      userAccount = web3.eth.accounts[0];

      , 100);






      web3js metamask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 28 at 9:15







      Marcellvs

















      asked Mar 27 at 10:31









      MarcellvsMarcellvs

      114 bronze badges




      114 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          In web3.js 1.x you must use getAccounts() async method, e.g. as follows:



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


          PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f55375031%2faccessing-accounts-in-metamask-through-web3-js-1-x%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In web3.js 1.x you must use getAccounts() async method, e.g. as follows:



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


            PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask






            share|improve this answer





























              0














              In web3.js 1.x you must use getAccounts() async method, e.g. as follows:



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


              PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask






              share|improve this answer



























                0












                0








                0







                In web3.js 1.x you must use getAccounts() async method, e.g. as follows:



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


                PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask






                share|improve this answer













                In web3.js 1.x you must use getAccounts() async method, e.g. as follows:



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


                PS: web3.eth.accounts[0] in other Dapps shows your account because they are still using the old web3.js version, very likely the one injected by Metamask







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 28 at 12:19









                saccoddsaccodd

                1222 silver badges11 bronze badges




                1222 silver badges11 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f55375031%2faccessing-accounts-in-metamask-through-web3-js-1-x%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

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해