Search code examples
javascriptreactjsapiexpress

Getting this error message 'TypeError: Failed to fetch'


While trying to fetch images from cloudinary getting this error 'TypeError: Failed to fetch'. Its a MERN project.

const fetchPosts = async () => {
  setLoading(true);

  try {
    const response = await fetch("http://localhost:8080/api/v1/post", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
    });

    if (response.ok) {
      const result = await response.json();
      setAllPosts(result.data.reverse());
    }
  } catch (err) {
    alert(err);
  } finally {
    setLoading(false);
  }
};

Solution

  • My IP address wasn't added to mongodb, once it was added the issue got resolved.