Fetch to my API from React Native is not working

146 Views Asked by At

I've been grappling with a peculiar issue that's been confounding me for several weeks. When I attempt to make a call to my API, it consistently fails to connect. However, I've observed that if I make a random API call to a different service before attempting to call my own API, it miraculously works.

To replicate this issue, I'm using React Native version 0.72.6. Specifically, I'm performing the following steps in the main App.js file:

I initiate a fetch request to my API endpoint at https://mindloop.tech:3001/users/test.

Upon doing so, I encounter the following error: "Possible Unhandled Promise Rejection (id: 0)."

The perplexing part is that when I uncomment the call to api.publicapis.org, something remarkable occurs. Suddenly, my initial API call works as expected. Subsequent calls to my API also work flawlessly. However, if I close the app and restart it, the same issue resurfaces.

In essence, my dilemma is this: Why does making an unrelated API call to api.publicapis.org before accessing my own API endpoint enable it to work temporarily, and how can I ensure that my API calls consistently function without this workaround?

Any insights or suggestions would be greatly appreciated. Thank you!

const apiTest = async () => {
  // const responseRandomAPI = await fetch('https://api.publicapis.org/entries',{
  //   method: 'GET',
  //   headers: { 'Content-Type': 'application/json',
  //   }
  // });
  // const jsonRandomAPI = await responseRandomAPI.json();
  const responseMyAPI = await fetch('https://mindloop.tech:3001/users/test',{
    method: 'GET',
    headers: { 'Content-Type': 'application/json',
    }
  });
  const jsonmyAPI = await responseMyAPI.json();
}
0

There are 0 best solutions below