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);
});
};