vcapretzV
Convex Community6mo ago
10 replies
vcapretz

Error throw after deleting entity in React Native

I'm building a React Native app with Expo router and my CRUD screens for entities are more or less like so:
List all entities screen -> View Entity screen -> Edit screen -> Delete button.

after calling the delete mutation, which in convex uses the .delete method, I use the router to dismiss back to the List screen but my "get(id)" query throws. I believe because Convex is still listening and the screen might be mounted somewhere

I don't know a clear and better path to do what I'm trying to accomplish, here are some code snippets:

my get single entity screen has the query:
export const get = query({
  args: { id: v.id("workoutTemplates") },
  handler: async (ctx, args) => {
    const userId = await getAuthUserId(ctx);

    if (userId === null) {
      throw new Error("Not authenticated");
    }

    const workout = await ctx.db.get(args.id);

    // THIS IS WHAT THROWS, probably because the screen is still mounted after I delete the record
    if (!workout || workout.personal !== userId) {
      throw new Error("Workout not found");
    }

    .... more code
    return {};
  },
});


delete mutation:
export const remove = mutation({
  args: { id: v.id("workoutTemplates") },
  handler: async (ctx, args) => {
    const userId = await getAuthUserId(ctx);

    if (userId === null) {
      throw new Error("Not authenticated");
    }

    const workout = await ctx.db.get(args.id);

    if (!workout || workout.personal !== userId) {
      throw new Error("Workout not found");
    }

    return await ctx.db.delete(args.id);
  },
});


and in my component I'm calling it like so:
mutateDelete({ id: templateId })
  .then(() => {
    router.dismissTo("/personal/workouts/templates");
  })
  .catch((error) => {
    console.log(error);

    toast({
      title: "Error when deleting",
      preset: "error",
    });
  });
Was this page helpful?