#Problem with typescript, using convex svelte quickstart example
6 messages · Page 1 of 1 (latest)
The error message you're seeing is related to TypeScript type checking. It seems that the "tasks" argument you're passing to ctx.db.query is not recognized as a valid table name in your data model.
In the Convex Svelte Quickstart example, a tasks.ts file is created in the convex/ folder with a query function that loads the data. The query function looks like this:
import { query } from "./_generated/server";
export const get = query({
args: {},
handler: async (ctx) => {
const tasks = await ctx.db.query("tasks").collect();
return tasks.map((task) => ({ ...task, assigner: "tom" }));
},
});
The error is occurring on the line where ctx.db.query("tasks").collect() is called. The "tasks" argument is not recognized as a valid table name in your data model.
This could be due to a few reasons:
-
The
taskstable does not exist in your database. Make sure you have created ataskstable in your Convex database. -
The
taskstable is not defined in your data model. If you're using TypeScript, you should define a schema for your data model. This is done in aschema.tsfile in theconvex/folder. Here's an example from the React Quickstart:
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
tasks: defineTable({
text: v.string(),
isCompleted: v.boolean(),
}),
});
In this example, a tasks table is defined with text and isCompleted fields.
- There's a mismatch between the
taskstable in your database and thetaskstable defined in your data model. Make sure the fields and their types match.
If you're still having trouble, you might want to check the Convex documentation (https://docs.convex.dev/quickstart/svelte) or ask for help in the Convex Discord community (https://discord.com/channels/1019350475847499849/1249729183890800650).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@elder heath kapa.ai is still learning and improving, please let us know how it did by reacting below
Let me check the knowledge base for you. One moment! :books:
Your schema definition is almost correct, but there's a small mistake. The defineSchema function expects an object where each key is a table name and each value is a table definition. In your case, tasks is already a schema, not a table definition.
Here's how you should define your schema:
import { defineSchema, defineTable } from 'convex/server';
import { v } from 'convex/values';
// Tasks table
const tasks = defineTable({
text: v.string(),
isCompleted: v.boolean(),
});
// Auth tables
const users = defineTable({
email: v.string(),
password_hash: v.string()
}).index('byemail', ['email']);
const sessions = defineTable({
id: v.string(),
user_id: v.id('users'),
expires_at: v.float64()
})
.index('byUserId', ['user_id'])
.index('byId', ['id']);
// Schema
const schema = defineSchema({
tasks,
users,
sessions
});
export default schema;
In this corrected version, tasks, users, and sessions are table definitions created with defineTable. These table definitions are then passed to defineSchema to create the schema.