#Deleting user

4 messages · Page 1 of 1 (latest)

tacit cairn
#

so I have a user stored in a table and I crated a delete feature so once the auth user is delted it give errors liek these so how do I handle it the convex way is there a way to unsubscrible ot all the convex connections to aovi these ?

Unhandled Runtime Error

Error: [CONVEX Q(modNotifications:getUnifiedNotifications)] [Request ID: 5ac17d02e72f8e83] Server Error
Uncaught Error: User not found
at handler (../convex/modNotifications.ts:331:10)

Called by client

quiet jayBOT
#

Hi @tacit cairn. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

When a user is deleted in Convex, any queries or subscriptions that depend on that user (such as fetching notifications for a user) may throw errors like "User not found" if they are not handled gracefully. This is a common scenario, especially in reactive apps where Convex queries are live and will rerun when the underlying data changes.

Convex Best Practices for Handling Deleted Users:

  1. Gracefully Handle Missing Users in Queries:
    Instead of throwing an error when a user is not found, return null or a default value. This prevents runtime errors when the user is deleted and the query reruns. For example:

    export const getUnifiedNotifications = query({
      args: { userId: v.id("users") },
      handler: async (ctx, args) => {
        const user = await ctx.db.get(args.userId);
        if (!user) {
          // Return null or an empty array instead of throwing
          return null;
        }
        // ...rest of your logic
      },
    });
    

    This approach is recommended by Convex support staff to avoid errors when a user is unauthenticated or deleted, as unauth can happen at any time and queries will rerun with no user context. If you want to avoid backend errors and make your app more robust, handle the missing user case without throwing an exception (Discord: How to correctly signOut with clerk).

#
  1. Frontend Error Handling:
    If you do want to throw an error (e.g., for a "not found" case), catch it on the frontend and handle it gracefully, such as by redirecting the user or showing a friendly message. Some developers use an ErrorBoundary or similar pattern to catch these errors and navigate away from the page (Discord: Error throw after deleting entity in React Native).

  2. Enhanced useQuery Hook:
    Consider using the enhanced useQuery hook from convex-helpers, which returns errors as objects rather than throwing, making it easier to handle these cases in your UI without disruptive error modals (convex-helpers: richer useQuery).

  3. No Need to Unsubscribe Manually:
    Convex's reactivity model means you don't need to manually unsubscribe from queries. Instead, ensure your queries can handle the "not found" case, and your UI will update automatically when the user is deleted.

Summary:
Handle missing users gracefully in your Convex queries by returning null or a default value instead of throwing. This is the Convex way to avoid errors when a user is deleted, and you do not need to manually unsubscribe from queries or connections.

If you need to handle this in the UI, use error boundaries or enhanced query hooks to catch and respond to these cases smoothly.

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