Cannot execute Moralis.enableWeb3()

Our application was working in plain javascript.

But when we checked now, it shows this error in console.

Uncaught (in promise) Error: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet.

How does your code look like?

I encountered the same error. It’s because somewhere in your code Moralis.enableWeb() is called more than once, and moreover at (almost) the same time.
It happened to me on a page that loads the same Component multiple times, where each Component was using this to init the contract:

    onMounted (async () => {
      const ethers = Moralis.web3Library
      const web3 = await Moralis.enableWeb3()
      try {
        contract.value = await new ethers.Contract(props.collection.attributes.token_contract, JSON.parse(props.collection.attributes.token_abi), web3)
        getBaseUri()
      } catch (err) {
        console.log(err)
      }
    })

The fix for me is not to call Moralis.enableWeb3() but use Moralis.web3 directly, as it was already initialized somewhere else:

    onMounted (async () => {
      const ethers = Moralis.web3Library
      const web3 = Moralis.web3
      try {
        contract.value = await new ethers.Contract(props.collection.attributes.token_contract, JSON.parse(props.collection.attributes.token_abi), web3)
        getBaseUri()
      } catch (err) {
        console.log(err)
      }
    })

Hi guys, I just wanna clarify something.

Does

await Moralis.enableWeb3()

have to be called every time you use executeFunction()?

or

Does it only have to be called once to enable Web3? If it only has to be initialized once, where should I instantiate it in my react page?

you can create a separate thread with this question

In my case it had actually worked but I was not seeing the provider because of incorrect async function syntax. After using β€œ.then()” correctly it worked. (https://www.w3schools.com/JS//js_async.asp). Then I still see the error one time but that must be due to the component being rendered more than 1 time at some place I’m not aware of.

If reactJs, you can do that on the wrapper component