t3 tRPC - CTX is undefined

Hey all. Finally checking t3 for the first time. Question. I was trying to add to the latest post query so that the latest post will be from the current user not all users.
const post = await ctx.db.query.posts.findFirst({
orderBy: (posts, { desc }) => [desc(posts.createdAt)],
where: (posts, { eq }) => eq(posts.createdById, ctx.session!.user.id),
});
const post = await ctx.db.query.posts.findFirst({
orderBy: (posts, { desc }) => [desc(posts.createdAt)],
where: (posts, { eq }) => eq(posts.createdById, ctx.session!.user.id),
});
<LatestPost> has a call to getLatest const [latestPost] = api.post.getLatest.useSuspenseQuery(); Which is called serverside first. I am confused why, for the first serverside call, ctx in trpc is null. To be fair this is fixed when void api.post.getLatest.prefetch(); is called in page.tsx But I do not understand why I have to prefetch getLatest for this to work? Apologies if this is not the place to ask this or my question does not make sense.
No description
No description
3 Replies
Hobbs
Hobbs5mo ago
did you end up figuring it out @Simon Verhoeven ?
Simon Verhoeven
Simon VerhoevenOP5mo ago
Hey @Hobbs no. I think I am experiencing something similar to this https://discord.com/channels/966627436387266600/988912020558602331/1270507000371085382 CTX is null for the first call. For now I am just either - Using server components to fetch the data (as this issue happens only on use client components) - Pre fetching in a parent server component seems to fix it But yeh I would still like to understand the issue here, why the sever render on a use client component has ctx undefined
Hobbs
Hobbs5mo ago
I think you may be looking for using something like the privateProcedure instead of the publiceProcedure
Want results from more Discord servers?
Add your server