Trust Wallet: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet

My Code

 try {
          this.user = await Moralis.authenticate({
            signingMessage: "Test"
          })
          window.ethereum.request({
            method: 'wallet_switchEthereumChain',
            params: [{
              chainId: '0x38'
            }],
          });
        } catch (error) {
          alert('Error: ' + error.message)
        }
      },

the error occur when new user ( unconnected wallet ) open my dapp from TrustWallet and click connect wallet but when the popup to connect wallet the user click back and then click connect again the error occur and they cant connect until page reload


any solution for this issue ?

Here’s a proper way to switching network with moralis: https://docs.moralis.io/moralis-dapp/web3/web3#switchnetwork
Here is a way to get the connected chain: https://docs.moralis.io/moralis-dapp/web3/web3#chainid
You need to know if the user is on the proper chain before switching.

And as the error, it seems you’re calling enableWeb3 multiple times, you can check for that

wow i dont know you can change network with moralis thanks for that , but the things is the main issue was the function authenticate

here example I create from vanilla moralis template
URL: moralis-trust-issue.surge.sh

step:

  1. open trust wallet with new address
  2. click “moralis metamask login”
  3. when popup occur you click back
  4. click again “moralis metamask login”
  5. error occur
    user cant connect again except they reload the trust wallet
1 Like