When to call await Moralis.enableWeb3()? Error

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

I’ve been getting this error consistently, and I’m unsure of how to “wait” for it to be finished before executing the next function.

I’ve been looking through the documentation, but it isn’t providing me with much information. I presume that the above await Moralis.enableWeb3() has to be called before every async Moralis function, right? For example, executeFunction.

useEffect(() => {
// Run for the first time
fetchWhiteList().then(()=>{
console.log(“Mint status updated updated!”);
fetchTotalSupply().then(()=>{
console.log(“Total Supply updated!”);
});
})

if (isLoading) {
  mint().then(() => {
    //fetchTotalSupply();
    // fetchWhiteList();
    fetchWhiteList().then(()=>{
      console.log("Mint status updated!")
      fetchTotalSupply().then(()=>{
        console.log("Total Supply updated!");
      });
    });
    setCounter(0);
    setLoading(false);
  });
}

}, [isLoading]);

Above is the hook that is causing the error. Just for clarification, useEffect is only executed when the mint button is pressed.

In fetchWhitelist, fetchTotalSupply and mint functions, they all have an await Moralis.enableWeb3() before the executeFunction is called.

So far you’re using reactjs, react-moralis gives you access to.

const { enableWeb3, isWeb3Enabled } = useMoralis();

You don’t need to call Moralis.enableWeb3 always
And on component mount of your wrapper component, you can consider doing this way.

useEffect(() => {
    if (!isWeb3Enabled) {
        enableWeb3();
    }
}, [isWeb3Enabled]);
1 Like

Hello! Thank you for your help, you helped me to solve my problem.

I wish to ask another question.

Because I’m using many functional components, is there any way I can make enableWeb3 persist across all pages, components?

Or is the only way to enableWeb3 in each component?

Thank you!

You can actually do that in your root component only most times in the app.js(x), and you can do some delay and some other checks (isWeb3Enabled) in other components’ function that require web3 to be enabled

1 Like