T
tRPC

❓-help

Can I redirect the user from inside my router?

Ooutis9910/17/2022
I have a query protectedProcedure, which returns an object from my prisma planetscale db What's the best way to redirect the user to a 404 page if the item doesn't exist on the db? I wanted something like res.redirect("/404") but can't seem to find a way to do it Should I just handle it in the front-end with a useEffect?
Ttomheaton10/17/2022
on your useQuery you can add an onError handler where you could redirect the user after raising a trpc error in your router when no item is found
Ooutis9910/17/2022
That might actually be what I want, thank you!
Ooutis9910/17/2022
client
const { data } = trpc.project.get.useQuery({ slug: String(router.query.slug) }, {
enabled: typeof router.query.slug === "string",
onError(err) {
console.log(err, 'error123')
//router.push("/404")
},
})
const { data } = trpc.project.get.useQuery({ slug: String(router.query.slug) }, {
enabled: typeof router.query.slug === "string",
onError(err) {
console.log(err, 'error123')
//router.push("/404")
},
})
server
get: protectedProcedure
.input(z.object({ slug: z.string() }))
.query(async ({ input, ctx }) => {
const project = await ctx.prisma.project.findFirst({
where: {
slug: input.slug,
user: {
email: ctx.session.user.email,
},
},
include: {
columns: {
include: {
cards: true,
},
},
},
});
if (!project?.id)
throw new TRPCError({ code: "NOT_FOUND", message: "eza" });
else return { project };
}),
get: protectedProcedure
.input(z.object({ slug: z.string() }))
.query(async ({ input, ctx }) => {
const project = await ctx.prisma.project.findFirst({
where: {
slug: input.slug,
user: {
email: ctx.session.user.email,
},
},
include: {
columns: {
include: {
cards: true,
},
},
},
});
if (!project?.id)
throw new TRPCError({ code: "NOT_FOUND", message: "eza" });
else return { project };
}),
Ooutis9910/17/2022
For some reason the query executes 4 times and only executes the onError function on the 4th time, which in total takes like 5 seconds Also I'm using enabled here to conditionally execute the query when the next/router is ready, if anyone has a better implementation would love to hear it

Looking for more? Join the community!

Recommended Posts
CORS in standalone serverHi, I'm trying to get CORS working in standalone server. Attempted this solution but unfortunatelyQueries break when in production on Vercel; work on localhostThe query is called like this: ```javascript const { data } = api.useQuery(["user.!getCount"], { Can't get client to workHi, I can't get my client to work I'm using Next.js but I have also tried the React method from the Request context inside middleware?Hi, Is it possible to get the request context inside a middleware somehow? I'm trying to migrate data becomes never[] when destructuring with a fallback valueCurrently it doesn't seem possible to set a fallback value on a destructured `data` property, for exSyntaxError Unexpected token u in JSON at position 0 with mutationsI'm trying to use trpc client to await a mutation and just keep getting this error. everything worksLosing the type between the client and serverHello everyone, I am new to tRPC and the magic of types in typescript so I am looking for ideas as tMobile app with tRPCWhat's the suggested way of working with tRPC when it comes to mobile apps? How do you ensure that yHow to use trpc react hooks from an external data sourceHi, I have a monorepo, nextjs and keystone cms. The cms has trpc running, and I managed to get it coNextJS & Keystone CMS, issues when connecting both in a monorepoHello everyone, so I have a monorepo with NextJS and keystone cms, both are running trpc v10beta.15Best practices for implementing an offline applicationHey there! I'm building a full stack react-native app with Expo and a tRPC backend. I'd like for thisomehow when move typed function out of the router files, frontend infer type to be any ?this is inferred corrected since they are in the same file. ``` getX: t.procedure.query(() => { retBig companies that use tRPC?Some of my fellow colleagues were wondering if there are any big companies that use tRPC and how matMigrating to V10 from V9If someone has some ideas on what I might have screwed up when trying to bump tRPC for this public s