Peacock
Peacockβ€’3d 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();
}
6 Replies
Convex Bot
Convex Botβ€’3d 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β€’3d 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β€’17h ago
Morning friends - any thoughts on this one? πŸ™‚
erquhart
erquhartβ€’9h 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β€’8h 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β€’6h ago
What’s your framework

Did you find this page helpful?