Truffle Metacoin – Fixing Metacoin Balance Not Showing in Dapp with Metamask

metamasktruffleweb3js

The sample Truffle Dapp doesn't work when Metamask active.
I'm running testrpc, I imported the tesrpc account's private key to Metamask and Metamask is connecting to the local testrpc according to chrome console log:

Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask

However Metamask doesn't show any ether balance on the account and the Dapp page shows empty string for Metacoin balance.

I debugged it and it seems web.3eth.getBalance never calls the callback function when Metamask is active:

  web3.eth.getBalance(myAccount, web3.eth.defaultBlock,
        function(error,result){
          if(error) {
            console.error("Error while retrieving the balance for address["+myAccount+"]: "+err);
          } else {
            var balance = web3.fromWei(result.toNumber());
            console.debug("Balance for address["+myAccount+"]: "+balance);

          }
        }
      ); 

It works perfectly when Metamask isn't active.

Best Answer

Reinstall Metamask, create a new Metamask account then import the same testrcp account private key will resolve the issue.

I'm unsure what was the cause. The same code started to work after Metamask reinstall. I did not even restart testrpc or redeployed the code.