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

Hi Moralis team,
I have one question.
While executing your moralis apis, I am getting this error:
Error: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet

So, how can I check in code whether Moralis.enableWeb3() is running or not?

Please let me know asap.

Best regards,

Try checking with Moralis.web3._isProvider. It will return true if already enabled.

what api are you calling? web3api doesn’t use/need an web3 instance


I am calling my contract function from contractProcessor.
You can see my some code for it above screenshot.
But, still react says Moralis.enableWeb3 is running.
How can I fix this error?
What is my mistake above code?

@cryptokid @johnversus
Can you help me asap on this ticket?

I’m not expert in react, at a first look I would think that you can call enableWeb3 hook from useMoralis() and also check with those specific hooks from there: isWeb3Enabled, isWeb3EnableLoading

and you could use useEffect to enableWeb3

@cryptokid

Still I have question.

const contractProcessor = useWeb3ExecuteFunction();
const ops = {
chain: process.env.REACT_APP_CHAIN_ID,
contractAddress: marketAddress,
functionName: “getSaleInfo”,
abi: contractABI,
params: {
startIdx: 0,
count: 100000
},
};
await contractProcessor.fetch({
params: ops,
onSuccess: (result) => {
console.log(“success:getSalesInfo”);
setSaleNFTs(result);
},
onError: (error) => {
console.log(“failed:getSalesInfo”, error);
setSaleNFTs([]);
},
});

I am running above code without connecting wallet.
But, I am getting the error:
failed:getSalesInfo Error: Missing web3 instance, make sure to call Moralis.enableWeb3() or Moralis.authenticate()

How can I fix?

did you try await Moralis.enableWeb3() in a separate button, or before this code?
assuming that no other code related to enableWeb3 was run

1 Like

I am running above code under the following code.

if ((isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) || !Moralis.web3 || !Moralis.web3._isProvider) {
if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) {
await Moralis.enableWeb3();
}
}

What’s wrong?

Since you’re using react, I will suggest you handle that in a useEffect function rather such like

useEffect(() => {
  if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) enableWeb3();
}, [isAuthenticated, isWeb3Enabled]);

Sorry, on bsc testnet, enableWeb3 is available without parameter like {provider: … }?
If it needs provider, because I am going to use without wallet, how can I pass?

image

Provider is optional. You enable web3 with any connector (such as WalletConnect, Metamask etc.), with the provider option.

If you trying to read a data you can use runContractFunction rather because useWeb3ExecuteFunction will require a connected wallet

I tried to use runContractFunction as you mentioned and docs.

import { useMoralisWeb3Api, useMoralisWeb3ApiCall } from “react-moralis”;

const NewItems = () => {
…
const { native } = useMoralisWeb3Api();
const ops = {
chain: process.env.REACT_APP_CHAIN_ID,
address: marketAddress,
function_name: “getSaleInfo”,
abi: contractABI,
params: {
startIdx: 0,
count: 100000
},
};
const { fetch: fetchSalesData, data: salesData, error: salesError, isLoading: isSalesLoading } =
useMoralisWeb3ApiCall(
native.runContractFunction,
{ …ops }
);
useEffect(async () => {
fetchSalesData({ params: ops });
}, []);

but, even though I put startIdx in params, it returns error code 141 - “startIdx is required”.
What’s up?
I need your help.


image

@qudusayo

Can you help me for this issue asap?

@qudusayo
I tried to use as the following:
const data = await Moralis.Web3API.native.runContractFunction(ops);

but, still the same error.

This function doesn’t need a web3 connection.

yeah, I tried as doc.
but, it returns above error.
https://docs.moralis.io/moralis-dapp/web3-api/native#runcontractfunction

You can try it this way

import { useApiContract, useMoralis } from "react-moralis";
const { isInitialized } = useMoralis();
const { runContractFunction } = useApiContract({
    address: marketAddress,
    functionName: "getSaleInfo",
    abi: contractABI,
    chain: process.env.REACT_APP_CHAIN_ID,
    params: {
      startIdx: 0,
    },
  });
useEffect(() => {
    isInitialized &&
      runContractFunction({
        onSuccess: (tx) => {
          console.log(tx);
        },
        onError: (error) => {
          console.log(error);
        },
      });
  }, [isInitialized]);

@qudusayo
Still the same error
{code: 141, error: “startIdx is required”}
code: 141
error: “startIdx is required”

Is there another way? or Moralis api issue?

Try use startIdx: “0”