I am trying to fetch available accounts from metamask, and it keeps returning null.
Environment:
web3 v1.0.0, npm 5.3.0, node 8.6.0, Rails 5.1.1, truffle 4.0.5.
Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<%= javascript_include_tag "web3.min.js" %>
<script>
$((function() {
var web3Provider
if (typeof window.web3 !== 'undefined') {
// if metamask is on, web3 is injected...
web3Provider = window.web3.currentProvider
} else {
// otherwise, use truffle network...
web3Provider = new Web3.providers.HttpProvider('http://localhost:9545')
}
console.log("web3Provider", web3Provider) // MetamaskInpageProvider
web3 = new Web3(web3Provider) // {givenProvider: MetamaskInpageProvider .. }
web3.eth.getAccounts(function(accounts) {
console.log(accounts) // null
})
}))
</script>
@woniesong92 it should be
web3.eth.getAccounts(function(error, accounts) {
console.log(accounts)
})
indeed!
I have the same issue:
var Web3 = require('web3');
var web3 = new Web3();
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.log('metamask used');
// Use Mist/MetaMask's provider
web3js = new Web3(web3.currentProvider);
} else {
console.log('No web3? You should consider trying MetaMask!')
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
web3js = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
web3js.eth.getAccounts(accounts => console.log(accounts[0]));
I see in the chrome console log that accounts variable is undefined
Thank you @iurimatias 馃憤
Most helpful comment
@woniesong92 it should be