#onError not triggering

9 messages · Page 1 of 1 (latest)

harsh saffron
#

Hi!

I know this looks insane. I am chaining multiple calls together (it's very temporary!). The onError: () => deleteFirstThing.mutateAsync(_data.id) isn't triggering. I know the call works. createThing.mutateAsync part returns a 403 atm. Any thoughts?

api.mutateAsync(request, {
                onSuccess: (data) => {
                    const _data = data?.data?.data;

                    
                    if (content.configuration.length > 0 && _data?.id) {
                        const request = generateRequest(_data.id,content.configuration);

                        createThing.mutateAsync(request, {
                            onError: () => deleteFirstThing.mutateAsync(_data.id)
                        });
                    }

                    onSuccess(_data);
                    closeOnSuccess();
                    setIsLoading(false);
                },
                onError: (error) => {
                    const backendErrors = handleBackendErrors(error);

                    setSubmitError(backendErrors);
                    setIsLoading(false);
                }
            });
harsh saffron
#

Is it possible Axios isn't throwing the error properly?

harsh saffron
#

Am I using mutateAsync wrong?

harsh saffron
#

adding async seem to fix it. I guess that makes sense

misty glade
#

I don't know if the callbacks are triggered when using mutateAsync. The docs recommend using a try/catch:

const mutation = useMutation({ mutationFn: addTodo })

try {
  const todo = await mutation.mutateAsync(todo)
  console.log(todo)
} catch (error) {
  console.error(error)
} finally {
  console.log('done')
}

https://tanstack.com/query/latest/docs/react/guides/mutations#promises

Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, TanStack Query exports a useMutation hook.

Here's an example of a mutation that adds a new todo to the server:

harsh saffron
#

Thanks Julien. That fixed the issue. Although in the codebase I'm working in I see mutateAsync work with onSuccess and onError

harsh saffron
#

Thanks msalsbery, it works now after adding async on the onSuccess wrapper.

api.mutateAsync(request, {
  onSuccess: async (data) => {
faint mason
#

mutate is literally the same as await muteAsync().catch(noop)

it's implemented exactly like that, so mutate and mutateAsync are the same - callbacks work with either one