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,050 points
390 views

4 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote
Best answer

I solved problem, using:

 web3.eth.getAccounts((err, res) => {                   
                   console.log(res[0]);
});
answered Oct 12, 2018 by Perry
• 16,450 points

selected May 7 by Omkar
+1 vote

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

answered May 7 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 by Kriti
–1 vote

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

web3.eth.getAccounts()

answered May 7 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,030 points
257 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
75 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,040 points
123 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
61 views
0 votes
1 answer
0 votes
1 answer

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

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,320 points
42 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
• 16,450 points

edited Jun 19, 2018 by Perry 209 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
• 16,450 points

edited Oct 11, 2018 by Omkar 214 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.