JeffWScott
JeffWScott5w ago

Getting 1 item by _ID from the DB.

This code seems to work compile and not throw any errors.
import { v } from "convex/values";
import { query } from "./_generated/server";

export const get = query({
args: {},
handler: async (ctx) => {
const chats = await ctx.db.query("chats").collect();
return chats
},
});

export const getOne = query({
args: {
chatId: v.id("chats")
},
handler: async (ctx, args) => {
const chat = await ctx.db.get(args.chatId)
return chat
},
});
import { v } from "convex/values";
import { query } from "./_generated/server";

export const get = query({
args: {},
handler: async (ctx) => {
const chats = await ctx.db.query("chats").collect();
return chats
},
});

export const getOne = query({
args: {
chatId: v.id("chats")
},
handler: async (ctx, args) => {
const chat = await ctx.db.get(args.chatId)
return chat
},
});
But I can't use getOne. This code:
import { useQuery } from 'convex-svelte';
import { api } from '$convex/_generated/api.js';

const query = useQuery(api.chats.getOne, {chatId: "jh7dtfhvyqp0qmmn7q4y3zhh6n7m8tcn"});****
import { useQuery } from 'convex-svelte';
import { api } from '$convex/_generated/api.js';

const query = useQuery(api.chats.getOne, {chatId: "jh7dtfhvyqp0qmmn7q4y3zhh6n7m8tcn"});****
tells me "Type 'string' is not assignable to type 'Id<"chats">'. Type 'string' is not assignable to type '{ __tableName: "chats"; }'" I just want to get a specific chat via that _id that is in the database... What am I doing wrong becvause all of the examples say this should work. getting all the chats works fine. please help!
3 Replies
Convex Bot
Convex Bot5w 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
erquhart5w ago
You need to assert the type when you're using a string, eg., as Id<'chat'>
JeffWScott
JeffWScottOP5w ago
thanks

Did you find this page helpful?