Next.js Discord

Discord Forum

is it correct to have an error like this ?

Unanswered
Briard posted this in #help-forum
Open in Discord
BriardOP
I want to handle errors, for example when executing this mutation an error appears

"use client";
etc
const mutationAdd = trpc.user.addUser.useMutation({
    onSuccess: async (data) => {
      await utils.user.getUsers.refetch();
      router.push("/admin/all-employees");
    },
    onError: (error) => {
      toast({
        variant: "destructive",
        action: <ToastAction altText="Try again">{error.message}</ToastAction>,
      });
      return;
    },
  });


server router
 addUser: publicProcedure
    .input(
      z.object({
        username: z.string(),
        name: z.string(),
        email: z.string().email(),
        password: z
          .string()
          .min(5, "password should be at least 5 digits long."),
        role: z.string(),
      })
    )
    .mutation(async ({ input }) => {
      const newUser = await auth.api.createUser({
        headers: await headers(),
        body: {
          name: input.name,
          email: input.email,
          password: input.password,
          role: input.role,
          data: {
            username: input.username,
          },
        },
      });
      return newUser;
    }),

1 Reply