Axios get giving network error

Asked

Viewed 11,168 times

2

I’m taking a course of React Active, but since my computer didn’t run Genymotion, I had to improvise and downloaded Bluestacks to debug the projects. The problem is that when my program requests from the internet, it always returns me Network Error. I am using the.

The whole mistake is this:

I’ve searched everywhere I can think of, but I can’t find anything to solve this problem.

Edit: had forgotten to put the code

main.js:

import React, { Component } from 'react';
import api from '../services/api';

import { View, Text } from 'react-native';

export default class Main extends Component {
    static navigationOptions = {
        title: 'JSHunt'
    };

    componentDidMount() {
        this.loadProducts();
    }

    loadProducts = async () => {
        const response = await api.get('/products');

        //const { docs } = response.data;

        //console.log(docs);
    };

    render() {
        return (
            <View>
                <Text>Página Main</Text>
            </View>
        );
    }
}

api.js

import axios from 'axios';

const api = axios.create({
  baseUrl: 'https://rocketseat-node.herokuapp.com/api'
});

export default api;
  • 1

    And where is the code in which you use the AXIOS? By the error message hints that an exception is being thrown and you are not capturing it, but you can not know without your code.

  • I forgot to put the code, now I edited it. Thanks for the touch!

1 answer

3


This error is happening due to a syntax error. When creating an instance of AXIOS, you should be using the property baseURL, nay baseUrl.

Another improvement you could make in code is to add a block try/catch, because the way it is, when a request fails, the only mistake you will receive is that an exception has been cast and not captured, which is not a very useful feedback to find out the reason for the error.

loadProducts = async () => {
    try {
        const response = await api.get('/products');

        //const { docs } = response.data;

        //console.log(docs);

    } catch(err) {
        // TODO
        // adicionar tratamento da exceção
        console.error(err);
    }
};
  • Thank you very much!!! There are a couple of days that I’m looking for the problem, I can’t believe it was a little writing mistake, and also thank you for the tip, I’m going to implement in the code.

Browser other questions tagged

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