1

I'm trying to connect to the blockchain via web3 and metamask and the web3.eth.getAccounts() method is returning undefined, however window.ethereum.request({ method: 'eth_requestAccounts' }) returns an array with the accounts.

This is my code:

const getWeb3 = () : Promise<Web3> =>
  new Promise<Web3>((resolve, reject) => {
    // Wait for loading completion to avoid race conditions with web3 injection timing.
    window.addEventListener("load", async () => {
      // Modern dapp browsers...
      if (window.ethereum) {
        const web3 = new Web3(window.ethereum);
        
        try {
          // Request account access if needed
            const test = await window.ethereum.request({ method: 'eth_requestAccounts' });
            console.log(test)// ["0x3b6f82ad02bf6820fe7062ae08817f54467bfb2a"]
         
          const accounts = await web3.eth.getAccounts();
          console.log(accounts) // undefined

          resolve(web3);
        } catch (error) {
          reject("User denied account access. " + error);
          console.log("User denied account access. " + error)
        }
      }
      // Legacy dapp browsers...
      else if (window.web3) {
        // Use Mist/MetaMask's provider.
        const web3 = window.web3;
        console.log("Injected web3 detected.");
        resolve(web3);
      }
      // Fallback to localhost; use dev console port by default...
      else {
        const provider = new Web3.providers.HttpProvider(
          "http://127.0.0.1:8545"
        );
        const web3 = new Web3(provider);
        console.log("No web3 instance injected, using Local web3.");
        resolve(web3);
      }
    });
  });

I need to use resolved web3 in another file, but right now everything that I try to access is returning undefined, for example, await web3.eth.net.getId();.

2 Answers 2

1

I end up solving this error by changing the version of web3, I was in version 0.20.x, and I changed it to the latest version, which was 1.4.0.

1

Do not use
web3.eth.getAccounts()
This has been deprecated. It may still be supported within the library for the legacy type of dapp browsers & wallets but it is recommended to use the new web3 method below.

The new version of Web3 recommends to use
web3.eth.requestAccounts()


Where you have declared

const test = await window.ethereum.request({ method: 'eth_requestAccounts' });

This is not a web3 method and while it could be used to load your dapp with the user's accounts, it may not work on all dapp browsers & wallets, especially older ones.


The last statement of yours about the undefined
web3.eth.net.getId()
This is incorrect. net is separate from eth
Try web3.net.getId() instead.


Also, if the function needs to query the blockchain for data, the function should be asynchronous or used with callbacks.

Hope this helps some!

  • @hashguide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.