Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
7 replies
sommeeeR

tRPC useQuery() or useMutation()

I have a component with a form containing a question about a secret name.

When submitting that form I want to validate that the user sent the correct secret name. What should I use? (the name will be in my environment variables)

Im currently trying to implement it with useMutation(). Is this the correct method to use?

This is what I have so far:
const { mutate } = api.userRouter.checkCatName.useMutation();
const handleSubmit = (values: { catName: string }) => {
    const res = mutate(values);
    console.log(res);
  };

its currently working, but i cant f.ex do console.log(res.result) I then get an error Cannot read properties of undefined. This is the procedure code:
  checkCatName: protectedProcedure
    .input(z.object({ catName: z.string().min(1).max(20) }))
    .mutation(({ input }) => {
      if (input.catName === 'secret') {
        return 'you did it!';
      }
      return 'you are bad';
    }),
Was this page helpful?