TanStackT
TanStack4y ago
9 replies
awake-maroon

Mutation onSuccess doesn't have ID returned from server

I'm trying to navigate to a newly created page on mutation success, but there is an ID missing.
const addDeliveryTypeMutation = useMutation({
    mutationFn: addDeliveryType,
    onSuccess: (data, variables, error, context) => {
      console.log('error', error)
      console.log('context', context);
      console.log('variables', variables);
      console.log('data', data);
      // queryClient.invalidateQueries('deliveryTypes');
      history.push(`/content/delivery/types/${data.id}`);
    },
  });

I could modify addDeliveryType response to change the ID field to a different name (like "newId"), but this is not ideal.
export const addDeliveryType = async (deliveryType) => {
  const response = await axios.post('/delivery/type', deliveryType);
  return { ...response.data, newId: response.data.id };
};

Any ideas on how to do that properly?
Was this page helpful?