Drasky VanderhoffD
Convex Community3mo ago
3 replies
Drasky Vanderhoff

onSuccess issue with CustomAction

I've been trying to use customAction and customQuery for adding a user check and inject the userId for filtering future queries and register actions with it. Honestly i'm lost at this point, i'm fairly new to Convex but i been working with it for 2 months, tried follow the examples in docs and check different ways to write it but nothing works. i have to add the skipLibCheck=true because there is a circular reference that makes no issue with the code base but types keeps on screaming.

Thank you so much to anyone that takes the time give it a look.

Code :
export const userAction = customAction(
  action,
  {
    args: {
      userId: v.optional(v.id("users")),
    },
    input: async (ctx, args) => {
      const identity = await ctx.auth.getUserIdentity();
      if (!identity) {
        throw new Error("Called without authentication present");
      }
      const user = await ctx.runQuery(internal.users.getUserIdentity, {
        tokenIdentifier: identity.tokenIdentifier,
      });
      if (!user) throw new Error("Authentication required");
      return {
        ctx: { user },
        args,
         // This was added and removed, either way i get the same error.
        onSuccess: ({ result }) => {
          console.info(`Action for user ${user.name} returned:`, result);
        }
      };
    },
  }
);

export const getUserIdentity = internalQuery({
  args: {
    tokenIdentifier: v.string(),
  },
  handler: async (ctx, { tokenIdentifier }) =>
    await ctx.db
      .query("users")
      .withIndex("by_token", (q) => q.eq("tokenIdentifier", tokenIdentifier))
      .unique(),
});


TSconfig :
{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["ES2021", "dom"],
    "forceConsistentCasingInFileNames": false,
    "module": "preserve",
    "moduleResolution": "bundler",
    "isolatedModules": false,
    "noEmit": true,
    "strict": false,
    "skipLibCheck": true
  },
  "include": ["**/*.ts", "../eslint.config.cjs"],
  "exclude": ["./_generated"],
}

Error :
node_modules/convex-helpers/server/customFunctions.ts:505:21 - error TS2339: Property 'onSuccess' does not exist on type '{ ctx: any; args: any; onSuccess?: (obj: { ctx: any; args: Record<string, unknown>; result: unknown; }) => void | Promise<void>; } | { ctx: any; args: any; onSuccess?: (obj: { ctx: any; args: Record<string, unknown>; result: unknown; }) => void | Promise<...>; } | { ...; }'.
  Property 'onSuccess' does not exist on type '{ args: {}; ctx: {}; }'.

505           if (added.onSuccess) {
                        ~~~~~~~~~
node_modules/convex-helpers/server/customFunctions.ts:506:25 - error TS2339: (same error)

506             await added.onSuccess({ ctx, args, result });
                            ~~~~~~~~~
node_modules/convex-helpers/server/customFunctions.ts:525:19 - error TS2339: (same error)

525         if (added.onSuccess) {
                      ~~~~~~~~~
node_modules/convex-helpers/server/customFunctions.ts:526:23 - error TS2339: (same error)

526           await added.onSuccess({ ctx, args, result });
                          ~~~~~~~~~
Was this page helpful?