#clerk delivers an object with keys like "_User" which throws a convex error

15 messages · Page 1 of 1 (latest)

crude mortar
#

@grizzled bolt clerk delivers an object with keys like "_User" which throws a convex error. the object from clerk looks like this

{
  data: [
    _User {
      id: 'user_2cAkrFJNEmOVuKQqLbKbj2glKcu',
      passwordEnabled: false,}

how can i get around this

grizzled boltBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Based on the knowledge sources provided, there isn't a direct solution to the issue you're facing with the "_User" key from Clerk causing a Convex error. However, there are some general practices and tips that might help you troubleshoot this issue:

  1. Error Handling: Convex provides a way to handle application errors by throwing a ConvexError which contains data that the client can receive and identify. You might want to check if the error you're getting is a ConvexError and if so, what data it contains. This might give you more insight into why the "_User" key is causing an issue. Source (https://discord.com/channels/1019350475847499849/1173177716967743598)

  2. User Data Management: When using Convex with Clerk, it's common to store user data in a Convex table, with a Clerk-compatible identifier as a secondary index. This might be a workaround to your issue, as you could store the user data you need from Clerk in a Convex table, and avoid the "_User" key causing an error. Source (https://discord.com/channels/1019350475847499849/1204788801420009523)

  3. Code Review: It's recommended to always understand your TypeScript errors (or lints) before you silence them, as they often indicate the root cause of the problem. Make sure you understand the error message you're getting and try to trace it back to its source. Source (https://discord.com/channels/1019350475847499849/1217859930292486196)

If you're still having trouble, you might want to reach out to the Convex or Clerk support teams for more specific assistance.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@crude mortar kapa.ai is still learning and improving, please let us know how it did by reacting below

crude mortar
#

@grizzled bolt according to your documentation you "Convex only supports "plain old JavaScript objects" (objects that do not have a custom prototype). Convex includes all enumerable properties. Objects can have at most 1024 entries. Field names must be nonempty and not start with "$" or "_"." which means you dont support javascript objects with keys like "_User", is that correct? the issue is, clerk delivers a javascript object with such keys. how do i get around this?

grizzled boltBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you're correct. According to the Convex documentation, field names in objects should not start with "$" or "_". This is likely the reason why you're encountering an error when trying to use the "_User" key from Clerk.

To get around this, you could create a new object and copy over the properties from the Clerk user object, changing the names of the keys that start with "_". Here's a simple example of how you might do this:

let clerkUser = /* the user object from Clerk */;
let convexUser = {};

for (let key in clerkUser) {
    if (key.startsWith('_')) {
        // Remove the leading underscore
        let newKey = key.substring(1);
        convexUser[newKey] = clerkUser[key];
    } else {
        convexUser[key] = clerkUser[key];
    }
}

In this code, convexUser is a new object that contains all the same properties as clerkUser, but any keys that started with "_" in clerkUser have had the underscore removed in convexUser.

Please note that this is a simple example and might not cover all edge cases. You should adjust this code to fit your specific needs.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@crude mortar kapa.ai is still learning and improving, please let us know how it did by reacting below

timid estuary
#

Those field name constraints I believe are only on the top-level fields. If you store it inside a nested object does it still fail @crude mortar ?

crude mortar
#

Im calling clerks ‘’clerkclient.users.getUserList()’’ via their node sdk in a convex action

#
import { createClerkClient } from '@clerk/clerk-sdk-node';
import { action } from './_generated/server';
import handleApiErrors from '../utils/handlesApiErrors';

const clerkClient = createClerkClient({
  secretKey: process.env.CLERK_API_SECRET_KEY
});

export const AdminGetUserList = action({
  args: {},
  handler: async (ctx, args) => {
    try {
      const res = await clerkClient.users.getUserList();
      const data = res.data.map((user) => {
        return {
          id: user.id,
          email: user.primaryEmailAddress?.emailAddress,
          firstName: user.firstName
        };
      });
      return data;
    } catch (err) {
      handleApiErrors({
        err: err,
        location: 'getUserList'
      });
    }
  }
});
timid estuary
#

ah yes, if you insert extra fields we don't silently discard them, whereas TypeScript types are comfortable with an api taking in a subset of an object's fields, or returning more fields than the return type specified. Was this failure during a db operation or argument validation?

timid estuary
#

ah, it was returning the data. gotcha