I am trying to replace a fetch
with axios
. I keep getting undefined in my console log.
JavaScript
x
32
32
1
async componentDidMount() {
2
console.log('app mounted');
3
4
const tokenString = sessionStorage.getItem("token");
5
const token = JSON.parse(tokenString);
6
7
let headers = new Headers({
8
"Accept": "application/json",
9
"Content-Type": "application/json",
10
'Authorization': 'Bearer ' + token.token
11
});
12
13
const response = await axios({
14
method: 'get',
15
url: Config.apiUrl + `/api/Orders/GetAllInvoices`,
16
headers: {
17
"Accept": "application/json",
18
"Content-Type": "application/json",
19
'Authorization': 'Bearer ' + token.token }
20
});
21
console.log(`axios: ${response.json}`)
22
this.setState({ invoiceList: response.json });
23
24
//const response = await fetch(Config.apiUrl + `/api/Orders/GetAllInvoices`, {
25
// method: "GET",
26
// headers: headers
27
//});
28
//const json = await response.json();
29
//console.log(json);
30
//this.setState({ invoiceList: json });
31
32
… 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?
Advertisement
Answer
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