dappuniversity / election

A Decentralized Ethereum Voting Application Tutorial
563 stars 506 forks source link

facing issue while connecting to MetaMask #39

Open KrishnaVeer7712 opened 4 years ago

KrishnaVeer7712 commented 4 years ago

when I enable MetaMask in crome and setup to required accounts, localhost web page is showing loading evin i had properly setup MetaMask as shown in image.

Capture333 But when I dissabled the MetaMask, It is showing proper output with account at index 0 as shown below: Capture444 truffle version Truffle v5.1.26 (core: 5.1.26) Solidity - 0.4.25 (solc-js) Node v12.13.0 Web3.js v1.2.1 Please help me getting out of this.

ullahwasim commented 4 years ago
  1. Add window.ethereum.enable(); after line 16(web3 = new Web3(web3.currentProvider);) in \src\js\app.js file
  2. Connect Metamask.
  3. Refresh Page
Maniddarky commented 3 years ago

Even after adding window.ethereum.enable() im getting the same Page with 'Loading....'

vasvi-sood commented 3 years ago

I'm getting the same error. Please help somebody!

introkun commented 3 years ago

Please update function initWeb3 in the app.js with the following:

  initWeb3: function() {
    if (typeof web3 !== 'undefined') {
      // If a web3 instance is already provided by Meta Mask.
      const ethEnabled = () => {
        if (window.ethereum) {
          window.web3 = new Web3(window.ethereum);
          window.ethereum.enable();
          return true;
        }
        return false;
      }
      if (!ethEnabled()) {
        alert("Please install an Ethereum-compatible browser or extension like MetaMask to use this dApp!");
      }
      web3 = window.web3;
      App.web3Provider = web3.currentProvider;
    } else {
      // Specify default instance if no web3 instance provided
      App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545');
      web3 = new Web3(App.web3Provider);
    }
    return App.initContract();
  },
introkun commented 3 years ago

@vasvi-sood could you please try my solution?

vasvi-sood commented 3 years ago

It is working with your code thanks. But the actual problem was that I needed to refresh the page, because metamask didn't connect automatically.

gc2000 commented 3 years ago

Thanks!