Next.js Discord

Discord Forum

trpc error

Unanswered
SharpieMaster posted this in #help-forum
Open in Discord
please help

18 Replies

account/page.tsx
import { redirect } from "next/navigation";
import React from "react";
import NavBar from "~/components/NavBar";
import NewRecipeButton from "~/components/NewRecipeButton";
import { api } from "~/trpc/server";

const AccountPage = () => {
    return (
        <div>
            <NavBar />
            <NewRecipeButton onClick={newRecipe} />
        </div>
    );
};

const newRecipe = async () => {
    "use server";

    console.log("new recipe");

    const newRecipe = await api.recipe.create.mutate({
        title: "title",
        description: "description",
    });

    console.log(newRecipe);
};

export default AccountPage;
components/NewRecipeButton.tsx
"use client";

import { FC } from "react";

interface NewRecipeButtonProps {
    onClick: () => Promise<void>;
}

const NewRecipeButton: FC<NewRecipeButtonProps> = ({ onClick }) => {
    return (
        <button
            onClick={async () => {
                await onClick();
            }}
        >
            new recipe
        </button>
    );
};

export default NewRecipeButton;
O.o sorry, you just sent that
recipe router:
import { z } from "zod";

import {
    createTRPCRouter,
    protectedProcedure,
    publicProcedure,
} from "~/server/api/trpc";

export const recipeRouter = createTRPCRouter({
    create: protectedProcedure
        .input(z.object({ title: z.string(), description: z.string() }))
        .mutation(async ({ ctx, input }) => {
            const recipe = await ctx.db.recipe.create({
                data: {
                    title: input.title,
                    description: input.description,
                    userId: ctx.session.user.id,
                },
            });

            return recipe;
        }),

    hello: publicProcedure
        .input(z.object({ message: z.string() }))
        .query(({ ctx, input }) => {
            return "hello" + input.message;
        }),
});
Error:
new recipe
 >> mutation  #1 recipe.create  { input: { title: 'title', description: 'description' } }
 << mutation  #1 recipe.create  {
  input: { title: 'title', description: 'description' },
  result: TRPCClientError: fetch failed
      at TRPCClientError.from (webpack-internal:///(rsc)/./node_modules/.pnpm/@trpc+client@10.37.1_@trpc+server@10.37.1/node_modules/@trpc/client/dist/TRPCClientError-fef6cf44.mjs:30:16)
      at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@trpc+client@10.37.1_@trpc+server@10.37.1/node_modules/@trpc/client/dist/httpBatchLink-520db465.mjs:207:101) {
    meta: undefined,
    shape: undefined,
    data: undefined,
    [cause]: TypeError: fetch failed
        at Object.fetch (node:internal/deps/undici/undici:11576:11) {
      cause: [RequestContentLengthMismatchError]
    }
  },
  elapsedMs: 14
}
- error node_modules\.pnpm\@trpc+client@10.37.1_@trpc+server@10.37.1\node_modules\@trpc\client\dist\TRPCClientError-fef6cf44.mjs (26:15) @ TRPCClientError.from   
- error TRPCClientError: fetch failed
null
account/page.tsx
import { redirect } from "next/navigation";
import React from "react";
import NavBar from "~/components/NavBar";
import NewRecipeButton from "~/components/NewRecipeButton";
import { api } from "~/trpc/server";

const AccountPage = () => {
    return (
        <div>
            <NavBar />
            <NewRecipeButton onClick={newRecipe} />
        </div>
    );
};

const newRecipe = async () => {
    "use server";

    console.log("new recipe");

    const newRecipe = await api.recipe.create.mutate({
        title: "title",
        description: "description",
    });

    console.log(newRecipe);
};

export default AccountPage;
Error occurs when I press the NewRecipeButton
using t3 stack
pnpm create t3-app@7.20.3-beta.61030b4
@riský O.o sorry, you just sent that
is this enough info?
looks good, only issue is i dont know much about trpc 😭
ah
@SharpieMaster ah
i dont see where an address is being set
the what now
are you running the trpc api separately from next
@SharpieMaster `pnpm create t3-app@7.20.3-beta.61030b4`
i do pnpm dev in my console, and it worked with the initial files
it has a content length mismatch. i assume its because its in a serverAction which is its own post request but thats a guess
same error if i do client side