reactjstanstackreact-query

useMutation returning null onError


I am using Tanstack query to make a PUT request. I am observing a 500 response in the network inspector, but when I log out the error in the onError function it returns null.

(Edit: to clarify, I want there to be an error - that's what I'm trying to handle currently in the code. But I expect something to be returned, not null)

Why is this happening? The only thing I can see that is different about this request compared to the other ones that catch the errors successfully is I am passing an extra argument in at the mutationFn step - does the Promise need extra handling here?

const apiCall = useMutation({
    mutationFn: async (payload) => apiRequest(id, payload),
    onSuccess: () => {
      console.log("success handling done here")
    },
    onError: ({ error }) => {
      console.log("Error returns null here")
    },
  })

Solution

  • For anyone who runs into this, the problem in this case was that the request was returning a void response type (204 on success) so it needed a format param adding to decode the error correctly.