Issue with getting web3 eth accounts MetaMask

0 votes

I am facing some issue with ethereum. web3.eth.accounts[0] or web3.eth.accounts is not working. When I tried console.log(web3.eth.accounts[0]), it returns undefined. But when I use console.log(web3.eth), I can see all the data in the console.

<html>
    <head>
        <title>TEST</title>
    </head>
    <body>
        <script>
            window.addEventListener('load', function(){
                if( typeof web3 !='undefined'){
                    console.log('web3 detected');
                    web3 = new Web3(web3.currentProvider);                                    
                    console.log(web3.eth.accounts);
                }else{
                    alert("please install MetaMask");
                }
            });
                       
        </script>
    </body>
</html>
Oct 12, 2018 in Blockchain by sabby
• 4,390 points
4,978 views

4 answers to this question.

+1 vote
Best answer

I solved problem, using:

 web3.eth.getAccounts((err, res) => {                   
                   console.log(res[0]);
});


Hope it helps!

Join our Blockchain course today to learn more about it.

Thanks.

answered Oct 12, 2018 by Perry
• 17,100 points

selected May 7, 2019 by Omkar
+1 vote

It is not working because web3.eth.accounts is not a list, it is a constructor. 

answered May 7, 2019 by Drake
+1 vote

If you are using web3js 1.0 then use this:

const web3 = new Web3(window.web3.currentProvider);
console.log(web3.eth.accounts)
answered May 7, 2019 by Kriti
–1 vote

Instead of using it as a variable, try using the function:

web3.eth.getAccounts()

answered May 7, 2019 by John
There's no such function

Related Questions In Blockchain

+1 vote
1 answer

Getting issues while Sending signTransaction with web3.js

The chain ID for ropsten is 3. ...READ MORE

answered Mar 28, 2018 in Blockchain by Johnathon
• 9,090 points
3,293 views
0 votes
1 answer
0 votes
1 answer

Why am I not getting 100 ethers in my metamask account?

Sorry that you're having this problem. To ...READ MORE

answered Jun 14, 2018 in Blockchain by Christine
• 15,790 points
2,252 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,350 points
3,662 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,663 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,129 views
0 votes
1 answer

Çannot implement web3.eth.subscribe on Web3 version 1.0.0-beta.27

As the error suggests, pub/sub is not ...READ MORE

answered Jun 19, 2018 in Blockchain by Perry
• 17,100 points

edited Jun 19, 2018 by Perry 1,730 views
+4 votes
2 answers

I am unable to connect Ganache with Truffle/Npm Dev server. Pls help.

The issue is at CurrentProvider. Give the url ...READ MORE

answered Mar 26, 2018 in Blockchain by Perry
• 17,100 points

edited Oct 11, 2018 by Omkar 2,193 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP