Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is axios not returning correctly

I am trying to replace a fetch with axios. I keep getting undefined in my console log.

async componentDidMount() {        
    console.log('app mounted');

    const tokenString = sessionStorage.getItem("token");
    const token = JSON.parse(tokenString);

    let headers = new Headers({
        "Accept": "application/json",
        "Content-Type": "application/json",
        'Authorization': 'Bearer ' + token.token
    });

    const response = await axios({
        method: 'get',
        url: Config.apiUrl + `/api/Orders/GetAllInvoices`,
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json",
            'Authorization': 'Bearer ' + token.token }
    });
    console.log(`axios: ${response.json}`)
    this.setState({ invoiceList: response.json });
    
    //const response = await fetch(Config.apiUrl + `/api/Orders/GetAllInvoices`, {
    //    method: "GET",
    //    headers: headers
    //});
    //const json = await response.json();
    //console.log(json);
    //this.setState({ invoiceList: json });
    ...

… the commented out fetch is working. I just now added the .json even though axios should not need it. Neither way works. What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Did you even console.log(response) just to see whats inside of it?

I guess you dont, because response is an object witch has no json key in it. You should use response.data

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading