Omar
Omar23h ago

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(),
}),
});
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);
},
});
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.
5 Replies
Convex Bot
Convex Bot23h ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart22h ago
import { asyncMap } from 'convex-helpers'
export const listMessages = query({
args: {},
handler: async (ctx) => {
const messages = await ctx.db.query("messages").order("desc").take(50);
return asyncMap(messages, async (message) => ({
...message,
user: await ctx.db.get(message.userId),
}));
},
});
import { asyncMap } from 'convex-helpers'
export const listMessages = query({
args: {},
handler: async (ctx) => {
const messages = await ctx.db.query("messages").order("desc").take(50);
return asyncMap(messages, async (message) => ({
...message,
user: await ctx.db.get(message.userId),
}));
},
});
Relevant docs here: https://docs.convex.dev/database/reading-data/#join Also highly recommend the tutorial to get up to speed on Convex: https://docs.convex.dev/tutorial/
Omar
OmarOP22h ago
convex-helpers? I don't recall finding a mention of it in the docs anywhere. I did read the docs but stumbled into another problem. Apparently, identity.subject is of type string, while message.userId expects an Id<>, so the solution you provided does not work in my case, and I'm not sure how can I fix this. And I did go through everything in the tutorial. Thanks for the suggestion regardless! And thanks for taking your time to help out.
erquhart
erquhart21h ago
Use getAuthUserId from convex-dev/auth/server instead of getUserIdentity The convex helper is just a little nicer than the vanilla js approach using Promise.all and array.map. They work the same.
Omar
OmarOP21h ago
Oh, I see. Thank you so much! It does indeed look a lot nicer. Thank you, much appreciated!