'req' of undefined in onError of express middleware
I've recently noticed that I get a bunch of errors regarding req object missing in ctx for onError property in trpc express middleware. Can't figure out how req obj might be undefined at the point of onerror handler??
5 Replies
It’s actually ctx which is undefined
Can you share your createContext? Also do you have any middlewares doing context swapping?
Ahh so seems I setup something in a wrong way coz I was sure ctx will have req/res at all times.
yeah sure here is my whole setup
context<typeof createTRPCContext>()
needs a inferAsyncReturnType<>
sprinkled in there, but might not be your issue here
Honestly this does look okay, it's a nice clean setup
enforceUserIsAuthed
also shouldn't need to add the req/res to context since you already did that
I'm really just finding silly things though, and am confused why you're getting an undefined context
If you're able to throw together a reproduction in stackblitz or something, a GitHub issue might be in orderYeah it will be hard to reproduce. No matter how hard I try I can't reproduce it locally. Happening only on Prod. I will try add a bit more logging maybe that will help me track down the problem.
That might be a more useful clue than you think
Figure out what's really different in prod, in many cases it's quite a lot!