bill92
bill928mo ago

Error calling middleware

I'm getting an error trying to test out some pretty basic middleware. This is my code
export const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => ({ prisma, res, req });

const t = initTRPC.context<Context>().create();

export const router = t.router;
export const procedure = t.procedure;

export const middleware = procedure.use(async (opts) => {
console.log('middleware');
return opts.next(opts);
});

export const authRouter = router({
me: middleware.query(async ({ ctx }) => {
console.log(ctx.req.headers.cookie);
return { success: true };
}),
});
export const createContext = ({
req,
res,
}: trpcExpress.CreateExpressContextOptions) => ({ prisma, res, req });

const t = initTRPC.context<Context>().create();

export const router = t.router;
export const procedure = t.procedure;

export const middleware = procedure.use(async (opts) => {
console.log('middleware');
return opts.next(opts);
});

export const authRouter = router({
me: middleware.query(async ({ ctx }) => {
console.log(ctx.req.headers.cookie);
return { success: true };
}),
});
Getting TypeError: Cannot read properties of undefined (reading 'use') I'm following the documentation [https://trpc.io/docs/server/middlewares] and I'm using @trpc/server@next
1 Reply
bill92
bill92OP8mo ago
any ideas? anyone?

Did you find this page helpful?