Iam trying to update two states inside axios.post method but while trying to login by clicking login button the page goes blank and when i comment or delete one of the state i.e setEmptyFields(error.response.data.emptyFields) or setError(error.response.data.message) it works but while using both it doesnot is there any way to handle this issue ??
const response = await axios
.post("http://localhost:4000/api/auth/login", loginUser)
.catch((error) =>
setEmptyFields(error.response.data.emptyFields)
setError(error.response.data.message)
);
If you're using the await
keyword, use a try/catch
.
try {
const response = await axios.post("http://localhost:4000/api/auth/login", loginUser)
//do something with the response...
} catch(error) {
setEmptyFields(error.response.data.emptyFields)
setError(error.response.data.message)
}
Alternatively you can use then
to handle the response:
axios
.post("http://localhost:4000/api/auth/login", loginUser)
.then((response) => {
//do something with the response...
})
.catch((error) =>
setEmptyFields(error.response.data.emptyFields)
setError(error.response.data.message)
);