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

3
  • can you show the result of console.log(error.response) inside your catch? Commented Jun 24, 2020 at 9:23
  • It's giving undefined!! Commented Jun 24, 2020 at 9: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: stackoverflow.com/a/9869609/339793 Commented Jun 24, 2020 at 10:17

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

No problem :)))
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?
Hi on ngrok's free plan the url generated is random and temporary. They have stated it in their documentation: ngrok.com/docs#getting-started-stable. Im not sure about the configuration details maybe the docs can help answer some of your questions.
1

If you are using dev build on a physical device you might always noticed a local IP address in the terminal or a QR code for it.

Just replace the localhost or 127.0.0.1 with that local IP but make sure to keep the port same as your backend server.

The best answer above here is an overhead with ngrok

3 Comments

Quick Question , did you ever find the answer to this, if so what was it ?
Basically, your server and the physical device must be on the same network, like I mentioned earlier, instead of using localhost or 127.0.0.1, you just need to use your local IP address that both devices are connected to
Ahhh yes perfect , this worked great, im just curious would the same apply for https server for external calls to backend, I have
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.

9 Comments

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.
for that, you have to mention the import statement in the file. without that its difficult to tell
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?
@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!!
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)?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.