DevThoughts
DevThoughts2y ago

Delete item {0: {json:{id: 12324}}}

When i try to mutate/delete item with id i am geting this payload {0: {json:{id: 12324}}}, without trpc everything working fine, dont understand where the {0: {json:{id: 12324}}} coming from? what is batch 1?
9 Replies
Nick
Nick2y ago
Have you enabled batching on the frontend but not the backend? This is a normal tRPC payload
DevThoughts
DevThoughtsOP2y ago
I did nothing with batching i have normal Nextjs app configured with TRPC. pages/api/[trpc].ts import { createNextApiHandler } from "@trpc/server/adapters/next"; import { env } from "~/env.mjs"; import { createTRPCContext } from "~/server/api/trpc"; import { appRouter } from "~/server/api/root"; // export API handler export default createNextApiHandler({ router: appRouter, createContext: createTRPCContext, onError: env.NODE_ENV === "development" ? ({ path, error }) => { console.error( ❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message} ); } : undefined, });
Nick
Nick2y ago
What does you client setup look like? If you're using httpBatchLink you have enabled batching on the frontend
DevThoughts
DevThoughtsOP2y ago
It look like this: iimport { httpBatchLink, loggerLink } from "@trpc/client"; import { createTRPCNext } from "@trpc/next"; import { type inferRouterInputs, type inferRouterOutputs } from "@trpc/server"; import superjson from "superjson"; import { type AppRouter } from "~/server/api/root"; const getBaseUrl = () => { if (typeof window !== "undefined") return ""; // browser should use relative url if (process.env.VERCEL_URL) return https://${process.env.VERCEL_URL}`; // SSR should use vercel url return http://localhost:${process.env.PORT ?? 3000}; // dev SSR should use localhost }; export const api = createTRPCNext<AppRouter>({ config() { return { transformer: superjson, links: [ loggerLink({ enabled: (opts) => process.env.NODE_ENV === "development" || (opts.direction === "down" && opts.result instanceof Error), }), httpBatchLink({ url: ${getBaseUrl()}/api/trpc, }), ], }; }, ssr: false, }); export type RouterInputs = inferRouterInputs<AppRouter>; export type RouterOutputs = inferRouterOutputs<AppRouter>; ` yes, then i can not understand when i am not able to delete item by it id.
Nick
Nick2y ago
Nick
Nick2y ago
You should enable batching on the backend I think it's actually on by default though What's your error exactly?
DevThoughts
DevThoughtsOP2y ago
I was getting undefind but not error, for some reason it is working now, but unclear why it didn't work. thank you! Btw, what is the best to send server error to the client, if i want to show toast or notification?
Nick
Nick2y ago
Error Formatting | tRPC
The error formatting in your router will be inferred all the way to your client (& React components)
DevThoughts
DevThoughtsOP2y ago
Is it a bad to create createTRPCRouter function for every route, like GetAllPost, create, delete, update ? or all the route should be in same file in createTRPCRouter ?