TanStackT
TanStack4y ago
5 replies
awake-maroon

Unhandled runtime error using useMutation hook

I am using useMutation hook to update my database records. I thought that useMutation hook handles the error with onError method, but instead I get unhandled runtime error, here is my code:
const updateDiary = async (updatedDiary: Day[], userId: string) => {
  const { error } = await supabase
    .from('current_diaries')
    .update({ days: updatedDiary })
    .eq('uid', userId);
  // this throws unhandled error
  if (error) {
    throw new Error(`${error.message}: ${error.details}`);
  }
};

export function useUpdateDiary() {
  return useMutation({
    mutationFn: async ({
      updatedDiary,
      userId
    }: {
      updatedDiary: Day[];
      userId: string;
    }) => {
      updateDiary(updatedDiary, userId);
    },
    onSuccess: () => {
      toast.success('Day updated');
    },
// this is not called on error
    onError: () => {
      toast.error(
        "We couldn't save your diary please check your internet  connection!"
      );
    }
  });
}
Screenshot_2022-11-20_at_12.44.36.png
Was this page helpful?