T
Join ServertRPC
❓-help
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?Have you enabled batching on the frontend but not the backend?
This is a normal tRPC payload
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,
});
What does you client setup look like?
If you're using httpBatchLink you have enabled batching on the frontend
It look like this:
return
};
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:
}),
],
};
},
ssr: false,
});
export type RouterInputs = inferRouterInputs<AppRouter>;
export type RouterOutputs = inferRouterOutputs<AppRouter>;
`
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 urlreturn
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.
You should enable batching on the backend
I think it's actually on by default though
What's your error exactly?
I was getting undefind but not error, for some reason it is working now, but unclear why it didn't work.
thank you!
thank you!
Btw, what is the best to send server error to the client, if i want to show toast or notification?
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
?