LukeL
Convex Community3y ago
45 replies
Luke

Best practices for joining data in query

I am attempting to join a project's notes to this query, I get no errors however the type inference insists that the property does not exist when I try to access it in the response.

export const getProjectById = query({
  args: { projectId: v.id("projects") },
  handler: withUser(async ({ db, user }, { projectId }) => {
    const project = await db.get(projectId);

    if (!project) {
      throw new Error("Project not found");
    }

    if (project.user !== user._id) {
      throw new Error("Unauthorized");
    }

    const notes = await db
      .query("notes")
      .withIndex("by_project", (q) => q.eq("project", project._id))
      .collect();

    return {
      ...project,
      notes: notes,
    };
  }),
});
Was this page helpful?