TanStackT
TanStack13mo ago
2 replies
inadequate-blush

I'm having an issue with an uncaught promise on a mutation

To my knowledge when you give a mutation function a promise Like a server action if it returns an error then you can use the on error and Onsuccess functions. I'm doing that all over my project and it's working just fine except in one section.
  const mutationAddQuestion = useMutation({
        mutationFn: async (formData: FormData) => {
            const correctAnswer = Array.from({ length: numberOfAnswers }).findIndex((_, index) =>     (formData.get(`correctAnswer${index}`) === 'on')) + 1;
            const bindFormWithData = addQuestion.bind(null, courseId, email, Number(correctAnswer));
            return await bindFormWithData(formData);
        },
        onError: (error) => {
            console.error(error);
        },
        onSuccess: () => {
            queryClient.invalidateQueries({ queryKey: ['courseQuestions', email, courseId] })
            queryClient.invalidateQueries({ queryKey: ['courseAnswers', email, courseId] })
            queryClient.invalidateQueries({ queryKey: ['courseCorrectAnswers', email, courseId] })
            afterAddQuestionOrCancel();
        },
    })

When this fails I the error logged in the console but i also get a mutation.js:156 Uncaught (in promise) Error: Error inserting question Error.
Every where else in my project when the action fails there is not uncaught (in promise warning). It causes the red warning toast from next js. else where in my project throwing an error from the action is caught and displays the warning fine.
To summarize I am throwing an error from my action and feeding it to Muse mutate function which is not handling the runtime exception for some strange reason I've tried making this into a api route but sending it an error code 500 still seems to have similar results. Hoping someone could point me in the right direction.
Was this page helpful?