JRPG AddictJ
Convex Community13mo ago
9 replies
JRPG Addict

Fetching another document's data via id in query

schema.ts
import { authTables } from "@convex-dev/auth/server";
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
  ...authTables,
  announcements: defineTable({
    text: v.string(),
    userId: v.string(),
  }),
  events: defineTable({
    title: v.string(),
    description: v.string(),
    date: v.string(),
    imageId: v.string(),
    userId: v.string(),
  }),
  messages: defineTable({
    text: v.string(),
    userId: v.string(),
  }),
});


chat.ts
import { v } from "convex/values";
import { mutation, query } from "./_generated/server";

export const sendMessage = mutation({
  args: { text: v.string() },
  handler: async (ctx, args) => {
    const identity = await ctx.auth.getUserIdentity();
    if (!identity) {
      throw new Error("Unauthenticated call to sendMessage");
    }
    const { text } = args;
    await ctx.db.insert("messages", {
      text,
      userId: identity.subject,
    });
  },
});

export const listMessages = query({
  args: {},
  handler: async (ctx) => {
    return await ctx.db.query("messages").order("desc").take(50);
  },
});

How do I read a user's name when querying the messages via the userId property? For context, I'm using ConvexAuth.
Was this page helpful?