async createContext for Express Adapter

Been debugging an odd behavior for the past hour, it seems like that an async function does not work in the express adapter. Is that supposed to work or is that the expected behavior? If this is a bug i can toss together a quick sandbox, but figured I check first whether this is expected or not. If this is expected behavior, should the async calls happen in middleware instead? Seems a bit at odds. Thoughts?
S
seN49415d ago
// Works router.use( '/trpc', trpcExpress.createExpressMiddleware({ router: appRouter, createContext: () => { user: 'test' }, }), ); // Does not work, user is always undefined router.use( '/trpc', trpcExpress.createExpressMiddleware({ router: appRouter, createContext: () => someAsyncFunction(); }), );
N
Nick415d ago
It’s worth putting together a reproduction, chances are it’s a mistake in your code, but if it’s reproducible then definitely raise a bug ticket on GitHub
S
seN49415d ago
Yeah, can't seem to reproduce in the code sandbox. Will have to dig some more into it. Speaking of codesandbox, I couldn't get the express example to work at all initially, adding node-fetch polyfill seems to fix it though: https://codesandbox.io/s/sleepy-booth-rl1xqk?file=/src/client.ts import fetch from "node-fetch"; // @ts-ignore globalThis.fetch = fetch; I'll open a pr for it later Just for completion sake... I was sure it had something to do with CORS i kept seeing undefined context and then delayed the correct context. Async didn t work, sync worked... The context code was something like this: () => { // do a bunch of stuff someFunction(() => {
return { user }; } return { user: undefined}; } tl;dr => there was another return path in the context from some earlier testing. Both returns would trigger eventually. but the initially one was the undefined, later on the internal function returned again haha
More Posts
Is it possible to split the router definition with the imlementation?I want to define the server router(input\output\meta) in a separate package from the server package Cache not working for `useQuery`I have a query like this: ```js const { data: article, isFetching } = api.public.getArticle.useQueryZod File Upload Validation with Open-Api Support?Hi guys, anyone know how to validate file upload with zod and get also open-api support?Zod validation and open-api support for File on the server?Hi guys, anyone know how to validate a File upload using zod? and also have open-api support?is context cached?If I put an object on the context that represents the User record from my database... ``` export asyJSON inferred router output not matchingHello. I have a procedure query which is returning Json from a postgresql db using Prisma. The type Best way to implement input based validation on a router procedureHi guys, bit of a noob. I have already created a 'protectedProcedure', ensuring the user is logged [Help] Turbo shared typesI have a turborepo with two apps (web and embed). web is a t3 stack and embed is a create-react-app.Cache SSG helper responseI'm using `createProxySSGHelpers` in GSSP with `ssr: false` in the global config. I trying to cacheInput is too big for a single dispatchI decided to try tRPC for my Crypto analytics dashboard. However, I'm having a hard time passing theHow to manage custom errors (e.g. custom error codes) in tRPC?What's the recommended way to add fields to a TRPCError? How do you make that typesafe also on the ctypesafe permissionsHi, So I wanted to infer all the procedures from my router recursively & assign a permission (string