I am working on a React JS project. I am using React query, https://react-query.tanstack.com/ to make API requests. I am now having a problem with mutation retrieving the response data from the server within the onError callback when the API throws 422 or 400 or 401, etc etc.
This is my mutation.
let [ createProductMutation, { data, error } ] = useMutation((payload) => createProduct(payload), {
onMutate: () => {
},
onSuccess: (response) => {
},
onError: (err, variables, snapshotValue) => {
//err is not the response from the server.
}
})
As you can see in the onError callback, there is no way to retrieve the response from the server.
The data and error (the one next to, createProductMutation) are not the response from the server too.
I tried using this.
try {
let response = await createProductMutation(data);
// response from the API is not here too, "response"
} catch (e) {
// response from the API is not here too
}
How can I retrieve the response within the onError callback?