Integrate your React Native application with a Node.js backend on your local machine

My attempt to establish a connection between my react native app and my node.js app on a Windows system has hit a roadblock. While I am able to receive responses from the node API using Postman, the response from the react native app is coming back as undefined.

IPv4 Address: 192.168.1.5

Node.js / index.js file

require('./config/mongoose')
require('dotenv').config()

const app = require('./app')
const port = process.env.PORT

app.listen(8000, '192.168.1.5')

React Native files

axios.js

import axios from 'axios';
import EncryptedStorage from 'react-native-encrypted-storage';

const axiosInstance = axios.create({
  baseURL: 'http://192.168.1.5:8000/api/',
  timeout: 10000,
});

axiosInstance.interceptors.response.use(
  function (response) {
    
  },
  function (error) {
    EncryptedStorage.removeItem('accessToken');
    EncryptedStorage.removeItem('user');
    axiosInstance.defaults.headers.common['Authorization'] = '';
    return Promise.reject(error);
  },
);

axiosInstance.defaults.headers.common['Content-Type'] = 'application/json';
axiosInstance.defaults.headers.common['Accept'] = 'application/json';

export default axiosInstance;

Function that calls the API

const signIn = async (username: string, password: string) => {
    let token = null;
    let user = {};

    console.log(username +'--'+ password);   

    await axiosInstance
        .post('login', {username, password})
        .then(response => {
            console.log(response);   
            let res = response.data;

            helpers.showToast(res.message);

            if (res.status) {
                // some other stuff
            }
        })
        .catch(e => {
            helpers.showToast(e.message);
            console.log('Error Signin: ', e.message);
        });
};

Answer â„–1

It's important to note that this issue is not related to networking, but rather a problem with axios interceptors. While Postman does not use axios or interceptors, your React Native code employs them and it appears that you forgot to include a return statement for the response in the interceptor:

axiosInstance.interceptors.response.use(
  function (response) {
    // Ensure to return the response
    return response;

  },
  function (error) {
    EncryptedStorage.removeItem('accessToken');
    EncryptedStorage.removeItem('user');
    axiosInstance.defaults.headers.common['Authorization'] = '';
    return Promise.reject(error);
  },
);

Answer â„–2

Have you experimented with using ngrok for testing?

npm install ngrok -global 

Next, open a separate terminal window. And execute :

ngrok http 8000 // the port your Node application is running on

An alternative link will be provided that can be used to test your Node application.

Copy the newly generated link, such as , and conduct tests to see if it resolves the issue

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Integrating threejs dynamically into a Create React App environment

When I am dynamically loading Three.js, the variable THREE is not found. I have created a React project using create-react-app and copied the three js file into the public folder. Here's the directory structure: src public ├── js │ └─┠...

What is causing my checkboxes to deactivate other selections?

My checkboxes are causing a dilemma where changing the value of one checkbox sets all others to false. To address this issue, I am considering implementing a solution in my validate.php file that would only update the intended checkbox value. If this appro ...

Custom value in Field for radio type in Redux form

Can anyone help me figure out how to input text into a radio field using Redux form? Here are the fields I am working with: <Field name={some.name1} value={'text1'} component={renderRadioElement} type="radio" /> <Field name= ...

Utilize ES6 syntax to bring in a package as an extension of another package

To expand map projections in D3, it is recommended to import the necessary packages like so: const d3 = require("d3") require("d3-geo-projection")(d3) This allows you to access methods such as d3-geo-projection's geoAiry method fr ...

Applying onclick css changes to a specific duplicate div among several others?

Recently, I encountered a situation where I have multiple identical divs on a page. <div class="class" ng-click="example()"></div> With the use of Angular 1.6.4 and jQuery, these divs are styled with background color and border color. Now, w ...

What is the process for exporting a plugin from dayjs() in JavaScript?

Currently, I have incorporated the plugin isToday() to enhance the capabilities of dayjs(). Nevertheless, I am uncertain about how to export isToday() in order to utilize it across other files. import isToday from "dayjs/plugin/isToday"; expor ...

Experience the convenience of uploading photos using jQuery Dialog on your ASP.NET MVC website

I am in the process of developing an ASP.NET MVC 3 application and encountering an issue with using a jQuery Dialog to upload a photo. Despite my code implementation, the HttpPostedFileBase object within my model (representing the file to be uploaded) cons ...

Utilizing Typescript for manipulation of Javascript objects

Currently, I am working on a project using Node.js. Within one of my JavaScript files, I have the following object: function Person { this.name = 'Peter', this.lastname = 'Cesar', this.age = 23 } I am trying to create an instanc ...

How to eliminate a particular item within a string only in rows that include a different specific item using JavaScript

Is my question clear? Here's an example to illustrate: let string = "Test text ab/c test Test t/est ### Test/ Test t/test Test test" I want to remove / only from the line that includes ###, like so: Test text ab/c test Test t/est ### Test T ...

What is the best way to enable the user to scroll smoothly while new data is continually being added to the screen?

I'm attempting to develop a chat feature where the scroll automatically moves down when new messages are received by the user. However, I've come across an issue while trying to allow users to manually scroll up. Every time I scroll up and a new ...

Getting the value of a lookup in an alert or console within a Material table in React

I am currently integrating a material table into my project and I have encountered an issue. Instead of getting the name of a city, I am receiving numbers like 63 or 32 in alerts or console logs. For reference, here is the link to the CodeSandbox: https:/ ...

Running a command once the forEach loop is completed in Angular

Within the HTML, I have a function that is triggered by an ng-click: vm.items = []; vm.moveItems = function() { angular.forEach(vm.items, function (item) { $http({ method: 'PUT', url: &apos ...

Error in External JavaScript File and Uncaught Reference Error

Wanting to utilize a separate Javascript file with my HTML code, I encountered an issue. Here is the code for the HTML document: <!DOCTYPE html> <html> <a href="javascript:showAlert()">Show Alert!</a> <script type="text/jav ...

Unable to accommodate additional space, InputGroup is not utilizing the

Using react-bootstrap in my project, I have a component with the following code. I want the input and button to display together and fill up the entire space allocated by the Col. Although the input and button are displaying side by side, they are not ta ...

Iterate over an array of objects to showcase the property values within an HTML tag using JavaScript

I am a beginner in JavaScript and I am currently working on an exercise. My goal is to iterate through an array of objects within another object, map the index values from one object to the id values in another object, and based on that, perform a certain ...

Challenges faced when using React Native with Redux and Navigation

Recently delving into React Native programming, I have discovered the power of Navigations. Despite being impressed by their capabilities, as a novice, I am facing some challenges. I am utilizing Redux for state management and have a Navigation Stack that ...

When working with angular.js and angular-sanitize.js, the src attribute is removed from JSON HTML data

I'm new to Angular and I'm really enjoying learning how to work with it. Currently, I have a simple JSON file that contains text structured like this: "gettingstarted":{ "title":"Getting Started", "content":"<img ng-src='i ...

Joi has decided against incorporating custom operators into their extended features

I am having trouble extending the joi class with custom operators. My goal is to validate MongoDB Ids, but when I try to use the extended object, I encounter the following error: error: uncaughtException: JoiObj.string(...).objectId is not a function TypeE ...

implement a discount and waive tax computation specifically for Canadian customers

I have encountered a problem while developing a POS application for a client in Canada. My issue lies in the tax calculation process, where I am unsure how to handle discounts and tax exemptions properly. Here is the scenario: I have 2 items - item 1 price ...

Alignment of Bootstrap thumbnails in a horizontal layout

After adding the thumbnail class to my thumbnails div, I noticed that some of the thumbnails were smaller in size than others. Wanting them all to have the same height, I decided to give each thumbnail a fixed height of 210px. However, this caused the sm ...