Need help showing message after fetch call if it fails
Unanswered
Laughing Gull posted this in #help-forum
Laughing GullOP
Hey Guys, new here.
Im following a tutorial about NextJS 13 and Zustand and im having a bit of trouble understanding, im hoping someone could help me out. I want to show a email verification message once a successful register is submitted
I have a register form that uses
My userService.register function looks like this;
The
When i hit submit, it registers the user successfully with the API, so thats all good, but if an error comes back from the API, for instance a 422 validation error, it still sets the setRegistrationSuccess to true and shows the email verification message. If i remove that line, then it shows the validation errors successfully.
I cant figure out a way to only set the
Im following a tutorial about NextJS 13 and Zustand and im having a bit of trouble understanding, im hoping someone could help me out. I want to show a email verification message once a successful register is submitted
I have a register form that uses
react-hook-form when submitting the form the onsubmit function looks like so;async function onSubmit(user) {
await userService.register(user)
setRegistrationSuccess(true)
}My userService.register function looks like this;
register: async (user) => {
alertService.clear()
try {
await fetch.post('/api/authentication/register', user)
} catch (error) {
alertService.error(error)
}
}The
alertService is using Zustand.When i hit submit, it registers the user successfully with the API, so thats all good, but if an error comes back from the API, for instance a 422 validation error, it still sets the setRegistrationSuccess to true and shows the email verification message. If i remove that line, then it shows the validation errors successfully.
I cant figure out a way to only set the
setRegistrationSuccess when the response is 2004 Replies
@Sun bear I think you need to verify !response.ok
Sun bear
On the try block throw an error when !response.ok with the response.status & response.statusText, Not sure about this but I think it gonna work
That happens because the catch only see a few errors of network, not the errors response of the API and you need to handle it
For that reason when you call the API you got a real response, an error for the API, but for the Catch block it is not