Reputation: 1528
My fetch request is as follows -
fetch(base_url + "/user/details", options)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error(response.json().message);
}
})
.then(responseJson => processResponse(responseJson))
.catch(error => handleError(error));
When the response is not ok
(400 in this case) the JSON looks like this -
{"timestamp":"2020-08-27T14:44:21.077176","message":"Failed for some reason."}
However, when I try to access the above response (like this - response.json().message
), I get a blank.
How do I access the JSON in this case?
Upvotes: 0
Views: 1208
Reputation: 944171
response.json()
returns a promise
It won't have a message
property, only its resolved value does.
const response = await fetch(base_url + "/user/details", options);
const data = await response.json();
if (response.ok) {
return processResponse(data);
}
handleError(new Error(data.message));
Upvotes: 2