Reputation: 152
In the catch block, Axios does not return errors in the form of JSON.
Signup page
const handleSignup = async () => {
try {
setIsLoading(true)
const res = await axios({
url: 'http://10.0.2.2:8080/signup',
method: 'POST',
data: { name, email, password },
})
Alert.alert(res.data.message)
} catch (err) {
console.error(err);
} finally {
setIsLoading(false)
}
}
Server side
return res.status(400).json({ data: { message: 'Email already exists.' } })
I expected it to return the error message 'Email already exists,' but it returned [AxiosError: Request failed with status code 400]. I've tried various approaches, but I'm not getting the expected result.
Upvotes: 1
Views: 692
Reputation: 8717
As per docs Handling Errors
access error response body in the catch block via error.response.data
in your case:
catch (err) {
console.error(err.response.data.data.message);
}
Upvotes: 1