Rune Darkfire
Rune Darkfire4mo ago

Getting orgId from userIdentity does not work

I can't seem to apply my org ID to newly created resources. I'm not sure what I'm doing wrong :
export const add = mutation({
args: {
orgId: v.optional(v.string()),
name: v.string(),
category: v.string(),
contactPerson: v.string(),
email: v.string(),
phone: v.string(),
address: v.string(),
website: v.string(),
description: v.string(),
status: v.string(),
riskScore: v.optional(v.number()),
totalSpend: v.optional(v.number()),
performance: v.optional(v.number()),
logoStorageId: v.optional(v.id("_storage")),
},
handler: async (ctx, args) => {
const user = await ctx.auth.getUserIdentity()

if (user === null) {
return null
}

const vendorId = await ctx.db.insert("vendors", {
...args,
orgId: user.orgId,
createdAt: Date.now(),
updatedAt: Date.now(),
})
return vendorId
},
})
export const add = mutation({
args: {
orgId: v.optional(v.string()),
name: v.string(),
category: v.string(),
contactPerson: v.string(),
email: v.string(),
phone: v.string(),
address: v.string(),
website: v.string(),
description: v.string(),
status: v.string(),
riskScore: v.optional(v.number()),
totalSpend: v.optional(v.number()),
performance: v.optional(v.number()),
logoStorageId: v.optional(v.id("_storage")),
},
handler: async (ctx, args) => {
const user = await ctx.auth.getUserIdentity()

if (user === null) {
return null
}

const vendorId = await ctx.db.insert("vendors", {
...args,
orgId: user.orgId,
createdAt: Date.now(),
updatedAt: Date.now(),
})
return vendorId
},
})
I have tried all manner of permutations of the orgId field above but it always results in an error either to do with not matching the schema (for the one above), or simply argument validation error with "Object is missing the required field 'orgId'" if I remove the optional, or remove the entry from the args altogether. I have added the claim for org ID in the JWT template that I'm using :
"org_id": "{{org.id}}"
"org_id": "{{org.id}}"
and it has saved/persisted in my Clerk config. From the guides I've read, this seems how I should be able to retrieve the org ID, but obviously something's wrong -- any help would be appreciated, thank you.
4 Replies
sshader
sshader4mo ago
Sounds like you've already tried this, but pointing out you have org_id (with an underscore) in your template and orgId (camel case) in your code, and I believe you want these to match. In general, I'm a fan of adding a bunch of logging + trying to simplify as much as possible. For instance, I'd probably start with a mutation that just logs ctx.auth.getUserIdentity() and see if that's what I'd expect. And I'd also potentially try even simpler changes to the clerk config (e.g. adding "foo": "bar" and seeing that the custom field makes it through to Convex) to narrow down exactly where the problem is
Rune Darkfire
Rune DarkfireOP4mo ago
how do you log the execution inside convex .ts files?
ballingt
ballingt4mo ago
Just convex.log() works well, in dev you'll see these in the browser. In dev and prod you can see these in the dashboard, and in the CLI npx convex logs.
Rune Darkfire
Rune DarkfireOP4mo ago
Thanks for the help @sshader @ballingt

Did you find this page helpful?