axios.get('/api/xyz/abcd')
.catch(function (error) {
if (error.response) {
// Request made and server responded
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
});
try {
const { data } = await axios({
method: 'put',
url: '/api/article/123',
data: {
title: 'Making PUT Requests with Axios',
status: 'published'
}
});
console.log(data);
} catch (err) {
if (err.response.status === 404) {
console.log('Resource could not be found!');
} else {
console.log(err.message);
}
}
const onSubmit = async (FormData) => {
FormData.bFlats = meters;
try {
let res = await axios({
method: 'post',
url: 'http://localhost:5000/api/v1/buildings',
data: FormData
});
console.log(res.data);
if (res.data.status === 'success') {
alert("Successfully Inserted");
reset();
}
} catch (error) {
console.log(error.response.data.message); // this is the main part. Use the response property from the error object
}
}
this issue happen when your response have an error , for example when the response is correct but in the then response you do something that throw an error , axios then catch that error , even if the response from the server was success