React Axios Network Error

Asked

Viewed 763 times

-1

I am doing my first course of Reset with Xios and found problems making get calls. The relevant code is: api.js:

import axios from "axios";

const api = axios.create({
  baseURL: "https://192.168.15.8:3333",
});

export default api;

index js.:

 async function loadIncidents() {
    if (loading) {
      return;
    }

    if (total > 0 && incidents.length === total) {
      return;
    }

    setLoading(true);

    const response = await api.get("incidents", {
      params: { page },
    });

    setIncidents([...incidents, ...response.data]);
    setTotal(response.headers["x-total-count"]);
    setPage(page + 1);
    setLoading(false);
  }

The mistake is:

Erro

  • in that part const response = await api.get("incidents", { missing bar, example const response = await api.get("/incidents", { because if you don’t put it points to the wrong address

  • I did what you said, keep on making the same mistake

  • and it is not possible to know! unfortunately problem that we can not reproduce

1 answer

-1

Check that your server IP has not been changed unintentionally.

When working with requests, or asynchronous functions (await) in general, always use Try{ ... } catch { ... } to handle the error:

async function loadIncidents() {
    if (loading) {
        return;
    }

    if (total > 0 && incidents.length === total) {
        return;
    }

    setLoading(true);
    try {
        const response = await api.get("incidents", {
            params: { page },
        });

        setIncidents([...incidents, ...response.data]);
        setTotal(response.headers["x-total-count"]);
        setPage(page + 1);
    } catch (err) {
        console.log('Ocorreu um erro: ', err)
    }
    setLoading(false);
}

This way you can display the error in a clearer way, and without crashing your application

Browser other questions tagged

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