Rune DarkfireR
Convex Communityβ€’17mo agoβ€’
4 replies
Rune Darkfire

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
  },
})
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}}"
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.
Was this page helpful?