T
TanStack3y ago
xenial-black

Why mutation queries are falling into 'success' instead of 'onError'?

const signUpMutation = useMutation({
mutationFn: ({signUpForm: signUpForm}) => signUpRequest(signUpForm),
onSuccess: (result, variables, context) => {
if(result.status && result.status === 400) {
// Should go to onError and goes here.
} else {
}
},
onError: () => {
showErrorToastMessage("The registration isn't allowed. ")
},
});


export const signUpRequest = async (formData) => {
return await fetch('/api/auth/signup', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
}).then(r => r.json());
};
const signUpMutation = useMutation({
mutationFn: ({signUpForm: signUpForm}) => signUpRequest(signUpForm),
onSuccess: (result, variables, context) => {
if(result.status && result.status === 400) {
// Should go to onError and goes here.
} else {
}
},
onError: () => {
showErrorToastMessage("The registration isn't allowed. ")
},
});


export const signUpRequest = async (formData) => {
return await fetch('/api/auth/signup', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
}).then(r => r.json());
};
2 Replies
xenial-black
xenial-blackOP3y ago
or should i handle non-200s responses in onSuccess anyway? what's the best practice here
exotic-emerald
exotic-emerald3y ago
To go to onError, the mutationFn must return a rejected Promise.

Did you find this page helpful?