skip to Main Content

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")
    },
  })

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. You should not destructure error from OnError

    const apiCall = useMutation({
          mutationFn: async (payload) => apiRequest(id, payload),
          onSuccess: () => {
            console.log("success handling done here");
          },
          onError: (error) => {
            console.log("Error:", error); // Logging the actual error
          },
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search