Possible Unhandled Promise Rejection (React Native (HOOKS))

Asked

Viewed 1,558 times

1

I am trying to call the API but is returning the warning error:

"Possible Unhandled Promise Rejection".

There are similar questions, but none specific to React Hooks from what I’ve seen.

Follow the codes

api.js

import axios from 'axios';

const api = axios.create({
    baseURL: 'http://localhost:3001/api'
});

export default api;  

Reportscreen.js

useEffect(() => {
    async function loadReports() {
        const response = await api.get('/reports');

        console.log(response.data);
    }

    loadReports(); 

}, []); 

Remark: on the backend already set the Cors and other necessary dependencies .

  • you’re sure the mistake is there?

  • As amazing as it sounds yes... It’s the only piece of Javascript, the rest of the code is graphical element

2 answers

1

In your Api.js file switch to

const api = axios.create({ baseURL: 'http://10.0.2.2:3001/api' });

and if you are using the expo

const api = axios.create({ baseURL: 'http://**ip do expo**:3001/api' });

check whether it will resolve the error

  • It actually worked by changing the baseURL, as you suggested. But now console.log(response.data); returns an empty array

  • Try to check in your API that everything is working right.

1


This Warning is why you are using the await without a try cacth

useEffect(() => {
    async function loadReports() {

        try {
           const response = await api.get('/reports');

           console.log(response.data);

        } catch(error) {
           console.log(error)
        }

    }

    loadReports(); 

}, []); 
  • Thanks! Solved the Warning, but keeps returning an empty array

  • Gives the console . log error to share with us

Browser other questions tagged

You are not signed in. Login or sign up in order to post.