Cloud function working locally but not in production

Hello,

Currently my project has a cloud function that queries all users and returns results.

I use the results to render wallet addresses to a profile card component.

The profiles load properly on localhost, but not in production. During deployment on Netlify, this message is logged: 20:6 Warning: React Hook useEffect has a missing dependency: 'fetch'. Either include it or remove the dependency array. react-hooks/exhaustive-deps

Here is the code:

 const [experts, setExperts] = useState(null);
  const { fetch } = useMoralisCloudFunction("getExperts");

  useEffect(() => {
    fetch({
      onSuccess: (data) => setExperts(data),
    });
  }, []);
  console.log(experts);

Thank you.

I’m not sure what is the issue, from that error message it looks like you could add that fetch dependency to useEffect, I don’t know if it really makes sense.

if you still don’t make it work, you can make a http request to that cloud function too

1 Like

Yea I added the word fetch to the dependency array. Nothing changed on the front end after deployment unfortunately.

How would I refactor to use http instead?

you can make a simple http request like this:

https://docs.moralis.io/moralis-dapp/cloud-code/cloud-functions#call-via-rest-api

That warning won’t be why it’s not working in production. If you wanted to fetch from the hook instead of using HTTP requests as soon as the component mounts, you should use a useEffect with isInitialized from the useMoralis hook:

const { isInitialized } = useMoralis();

useEffect(() => {
  if (isInitialized) {
    fetch({
      onSuccess: (data) => setExperts(data),
    });
  }
}, [isInitialized]);

Make sure your app works when built locally (npm run build) and then served with a local webserver before deploying to a platform like Netlify or Vercel.

1 Like