TypeError: Cannot read properties of undefined (reading 'query') #1896
-
Hi everyone, I am using Vike + tRPC + Hono.js to build an SSR application, but I have encountered some problems.
Here is the relevant code: import { initTRPC } from "@trpc/server"
import { Context } from "./context"
import { z } from "zod"
import { authRouter } from "./router/auth"
/**
* Initialization of tRPC backend
* Should be done only once per backend!
*/
const t = initTRPC.context<Context>().create()
/**
* Export reusable router and procedure helpers
* that can be used throughout the router
*/
export const router = t.router
export const publicProcedure = t.procedure
export const appRouter = router({
demo: publicProcedure.query(async () => {
return { demo: true }
}),
createTodo: publicProcedure
.input(z.string())
.mutation(async (opts) => {
console.log("Received new todo from " + opts.ctx.user?.name, { text: opts.input })
}),
auth: authRouter // Adding this line will cause error
})
export type AppRouter = typeof appRouter
import { router, publicProcedure } from '../server'
export const authRouter = router({
list: publicProcedure.query(() => {
return []
}),
})
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"
import { Hono } from "hono"
import { appRouter } from "./trpc/server"
import { createContext } from "./trpc/context"
const app = new Hono()
app.all('*', (c) => {
return fetchRequestHandler({
endpoint: '/api/trpc',
req: c.req.raw,
router: appRouter,
createContext
})
})
export const trpcRouter = app Here are the individual dependencies I use:
I've done some searching and trying but I don't really know how to fix this, has anyone had a similar problem please? Edit: This error will still appear after run
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm not sure why, but putting the appRouter in a separate _app.ts file eventually solved the problem. |
Beta Was this translation helpful? Give feedback.
I'm not sure why, but putting the appRouter in a separate _app.ts file eventually solved the problem.
https://trpc.io/docs/server/merging-routers#merging-with-child-routers