xeon06
xeon063y ago

input needs to be an object when doing a batch call?

Hey folks, just setup tRPC and running into the above error when trying to run mutations. Queries, even with inputs, work fine. Am I doing something wrong?
// Router
export default router({
submit: procedure
.input(
z.object({
foo: z.string(),
})
)
.mutation(({ input }) => {
console.log(input)
return "helloooo"
}),
})

// Component
const submit = trpc.submit.useMutation()

submit
.mutateAsync({ foo: "bar" })
.then((data) => console.log(data))
// Router
export default router({
submit: procedure
.input(
z.object({
foo: z.string(),
})
)
.mutation(({ input }) => {
console.log(input)
return "helloooo"
}),
})

// Component
const submit = trpc.submit.useMutation()

submit
.mutateAsync({ foo: "bar" })
.then((data) => console.log(data))
7 Replies
xeon06
xeon06OP3y ago
If I look at the request, it's a POST request with a ?batch=1 query param. The body is {"0":{"foo":"bar"}}
Nick
Nick3y ago
This all looks fine Could you show the exact error? And share any more complete code? Stripping out the gist of the code for an example often seems helpful but when it's likely to be a coding mistake it actually hides useful information from us
xeon06
xeon06OP3y ago
The above is copy pasted verbatim, except that the mutateAsync call happens in an onClick handler... Full(er) error:
POST http://localhost:3000/trpc/submit?batch=1 400 (Bad Request)

react_devtools_backend.js:4012 TRPCClientError: "input" needs to be an object when doing a batch call
at TRPCClientError.from (transformResult-6fb67924.mjs:4:20)
at httpBatchLink.mjs:190:56

transformResult-6fb67924.mjs:4 Uncaught (in promise) TRPCClientError: "input" needs to be an object when doing a batch call
at TRPCClientError.from (transformResult-6fb67924.mjs:4:20)
at httpBatchLink.mjs:190:56
POST http://localhost:3000/trpc/submit?batch=1 400 (Bad Request)

react_devtools_backend.js:4012 TRPCClientError: "input" needs to be an object when doing a batch call
at TRPCClientError.from (transformResult-6fb67924.mjs:4:20)
at httpBatchLink.mjs:190:56

transformResult-6fb67924.mjs:4 Uncaught (in promise) TRPCClientError: "input" needs to be an object when doing a batch call
at TRPCClientError.from (transformResult-6fb67924.mjs:4:20)
at httpBatchLink.mjs:190:56
Maybe it has to do with the way my server is setup...
Nick
Nick3y ago
Could be, we’re into some weeds that I’m not too familiar with here
xeon06
xeon06OP3y ago
All good, I think it might be my server setup. It's a bit unusual. Thanks for the help!
Nick
Nick3y ago
Might be worth trying one of the example/starter projects and bringing over some work to or from it. Would give you a basis for comparison at least! Maybe someone else here recognises the issue though, good luck!
xeon06
xeon06OP3y ago
The body that's coming in from my server is of 0 length so looks like that's the problem 🙃 Thanks again!

Did you find this page helpful?