Peacock
Peacockβ€’3mo ago

Clerk convex logic throws an error every first-time I save while running dev server

6/3/2025, 9:52:40 PM [CONVEX Q(projects:fetchProjects)] Uncaught Error: Can't get current user
at getCurrentUserOrThrow (../../convex/users.ts:57:9)
at async handler (../convex/projects.ts:10:15)
6/3/2025, 9:52:40 PM [CONVEX Q(projects:fetchProjects)] Uncaught Error: Can't get current user
at getCurrentUserOrThrow (../../convex/users.ts:57:9)
at async handler (../convex/projects.ts:10:15)
I consistently see the function provided on the Clerk+Convex documentation throw on first save, when any convex files have been modified. If I CMD+S again (without file changes) I see the happy message. Odd - I'm just now noticing any time there's an error, the time on save is significantly higher:
βœ” 21:52:06 Convex functions ready! (991.28ms) 6/3/2025, 9:52:06 PM [CONVEX Q(projects:fetchProjects)] Uncaught Error: Can't get current user at getCurrentUserOrThrow (../../convex/users.ts:57:9) at async handler (../convex/projects.ts:10:15) βœ” 21:52:26 Convex functions ready! (394.91ms) βœ” 21:52:32 Convex functions ready! (379.47ms) βœ” 21:52:40 Convex functions ready! (947.86ms) 6/3/2025, 9:52:40 PM [CONVEX Q(projects:fetchProjects)] Uncaught Error: Can't get current user at getCurrentUserOrThrow (../../convex/users.ts:57:9) at async handler (../convex/projects.ts:10:15)
Any thoughts why? The functionality of the Clerk webhook + seeding the DB works great, annoying to see these errors on the console though πŸ™‚ Below is the code from the docs that's throwing.
export async function getCurrentUserOrThrow(ctx: QueryCtx) {
const user = await getCurrentUser(ctx);
if (!user) throw new Error("Can't get current user");
return user;
}

export async function getCurrentUser(ctx: QueryCtx) {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
return null;
}
return await userByExternalId(ctx, identity.subject);
}

async function userByExternalId(ctx: QueryCtx, externalId: string) {
return await ctx.db
.query("users")
.withIndex("byExternalId", (q) => q.eq("externalId", externalId))
.unique();
}
export async function getCurrentUserOrThrow(ctx: QueryCtx) {
const user = await getCurrentUser(ctx);
if (!user) throw new Error("Can't get current user");
return user;
}

export async function getCurrentUser(ctx: QueryCtx) {
const identity = await ctx.auth.getUserIdentity();
if (identity === null) {
return null;
}
return await userByExternalId(ctx, identity.subject);
}

async function userByExternalId(ctx: QueryCtx, externalId: string) {
return await ctx.db
.query("users")
.withIndex("byExternalId", (q) => q.eq("externalId", externalId))
.unique();
}
18 Replies
Convex Bot
Convex Botβ€’3mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Peacock
PeacockOPβ€’3mo ago
Yeah, here is more fuel to that fire - hit CMD+S a few times in a row, no change- quick turn. Added a simple LOC to an unrelated file (not to users.ts or projects.ts) and this throws, also took longer. hit CMD+S a few times again, no change- quick turn. Removed the simple LOC from that unrelated file (not users.ts or projects.ts) and it throws again, also took longer.
No description
Peacock
PeacockOPβ€’3mo ago
Morning friends - any thoughts on this one? πŸ™‚
erquhart
erquhartβ€’3mo ago
I need to see more code to understand what's happening here. The query could be rerun by your client depending on how your local dev server watch is working. Convex will only rerun if something about the query changes, could be a helper function, or a shared dependency. Can't get current user def means you don't have a user. Where are you calling the function from? Any server side/middleware calls? If it's client, are you skipping until you have an authenticated signal from isAuthenticated or the Authenticated component?
Peacock
PeacockOPβ€’3mo ago
That's just it - this is calling when my frontend is completely turned off. so I don't have a damn clue what's calling these functions to run haha!
erquhart
erquhartβ€’3mo ago
What’s your framework
Peacock
PeacockOPβ€’3mo ago
tanstack start (not alpha branch)
erquhart
erquhartβ€’3mo ago
It's definitely being triggered by your code, maybe a zombie process still running a dev server or something? At any rate, I'd search the codebase for fetchProject and catalog everywhere it's being called. You can log from each call site to figure out where it's being called from. I know these are very elementary debugging steps, but it's what I'd do next personally.
Peacock
PeacockOPβ€’3mo ago
I'll look again - but quite honestly I'd be shocked... this is a brand new function that hasn't been integrated into the app yet. I'm only now switching to convex πŸ˜„
erquhart
erquhartβ€’3mo ago
Yeah I understand, but you're also in mystery territory, so whatever the culprit is will be a surprise. Somewhere something is calling this function, and Convex won't call your functions by itself. I'd grep "fetchProjects" across the codebase to find the culprit.
Peacock
PeacockOPβ€’3mo ago
does convex try to analyze or run anything on save when running convex dev server that might be kicking these off?
erquhart
erquhartβ€’3mo ago
It doesn't run functions. Maybe if you had a top level expression in one of your convex files that executes a Convex function, but you would see that if you grepped 'fetchProjects' Did you grep and not find anything?
Peacock
PeacockOPβ€’3mo ago
Nothing, just the declaration itself I’ll keep digging!
erquhart
erquhartβ€’3mo ago
Yeah that's super odd. It does sound a little like a zombie process or a tab running outdated code or something
Peacock
PeacockOPβ€’3mo ago
What throws me off is I haven’t changed any of my app code yet.
erquhart
erquhartβ€’3mo ago
Where did the code that's running come from, a starter? Can you briefly walk through the steps you took from not using Convex to your current project state
Peacock
PeacockOPβ€’3mo ago
I began with the clerk convex starter, and then started to write additional APIs for convex. Will debug more tomorrow, birthday today 🍻🍻
erquhart
erquhartβ€’3mo ago
Ayyy happy birthday! 🍻

Did you find this page helpful?