1

I am getting a Network Error while calling my server(nodejs) API.

Error Message and the function call.

I have rechecked my localhost, server, and port. They are all working fine. I have also confirmed using POSTMAN and also ran the server on the web locally to fetch data.

Postman and web data

I think I have also added the cors properly in my server.js file.

server.js file

Want some help on this.

I have also tried with dummy API and they worked fine.

My imports from front-end

Imports

Here is LogCat

enter image description here

enter image description here

Response Header in Postman

enter image description here

ngrok status

enter image description here

Satyam
  • 567
  • 1
  • 6
  • 20
  • can you show the result of `console.log(error.response)` inside your catch? – Naresh Jun 24 '20 at 09:23
  • It's giving undefined!! – Satyam Jun 24 '20 at 09:49
  • @Satyam The errors returned here are probably pretty useless. If you're on Android, you might try looking at LogCat. See this post here on how to filter relevant messages: https://stackoverflow.com/a/9869609/339793 – Philipp Sumi Jun 24 '20 at 10:17

3 Answers3

0

It seems that you are having a problem in react native code, not in backend. That's why you are not getting any error in postman.

I am guessing that you are importing the dependencies is problematic way.

prisar
  • 3,041
  • 2
  • 26
  • 27
  • Thank You for your response can you just point me out particularly which dependency? I have used a dummy API and logged that on the console using Axios, which worked fine for me. – Satyam Jun 24 '20 at 08:50
  • for that, you have to mention the import statement in the file. without that its difficult to tell – prisar Jun 24 '20 at 09:08
  • A common fix here is to use the actual IP address rather than `localhost`. Did you already try that? If that's not helping: I just ran into this issue a day ago - for me, it turned out to be a versioning conflict with OkHttp3, to which I had a dependency. Would that be the case for you? – Philipp Sumi Jun 24 '20 at 09:22
  • @PhilippSumi Yes, I have tried the IP address, I am getting the same error. I think I can try with OkHttp3. Can you just share a link or some info on the same. Thank You!! – Satyam Jun 24 '20 at 09:33
  • @Satyam here's the link to my issue: https://stackoverflow.com/q/62532912/339793. Maybe also try the mock APIs I posted there - maybe it helps narrowing it down. – Philipp Sumi Jun 24 '20 at 10:14
  • @prisar I have added the headers. I don't know if you meant the same or something else. I am kind of new to this. – Satyam Jun 24 '20 at 11:45
  • @PhilippSumi I tried to add those api in my app **build.gradle** but still its the same. I have also uploaded the log of the same error. I didn't get that, a bit confusing. – Satyam Jun 24 '20 at 11:47
  • 1
    maybe we're looking at multiple issues here. The `localhost` bit is still a bit fishy - after all, `localhost` for your device is something else then your machine (not sure how it is in an AVD). The IP address may not work if there's no listener process (for IIS for example, I have to proxy it in order to have my app talk to it when it's running locally. Can you try accessing your API through a regular URL (e.g. through ngrok)? – Philipp Sumi Jun 24 '20 at 12:00
  • @PhilippSumi I tried that, I have posted the same. I think it's working fine – Satyam Jun 24 '20 at 12:23
0

Hi I think you can try exposing your local host to the internet using a service like ngrok which will generate a url for you, and then try using the url to send requests instead of localhost:3000. Simply download and follow the instructions here: https://ngrok.com/docs

Roy Chan
  • 287
  • 3
  • 10
  • No problem :))) – Roy Chan Jun 24 '20 at 12:42
  • A bit of confusion here!! How to make it permanent? Like is there any configuration to just run it on a single command(script). Now it is like running 3 ports for the same. And how to configure the generated URL? – Satyam Jun 24 '20 at 12:45
  • 1
    Hi on ngrok's free plan the url generated is random and temporary. They have stated it in their documentation: https://ngrok.com/docs#getting-started-stable. Im not sure about the configuration details maybe the docs can help answer some of your questions. – Roy Chan Jun 24 '20 at 12:51
0

if you are trying to use local server API in mobile application development, all you need to do is replace "localhost" in the URL with your pc IP address. This worked for me.