#Problem with typescript, using convex svelte quickstart example

6 messages · Page 1 of 1 (latest)

eternal orioleBOT
#

Hi @elder heath. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

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:

  1. The tasks table does not exist in your database. Make sure you have created a tasks table in your Convex database.

  2. The tasks table 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 a schema.ts file in the convex/ 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.

  1. There's a mismatch between the tasks table in your database and the tasks table defined in your data model. Make sure the fields and their types match.
#

@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.

For more information, you can refer to the Convex documentation on writing schemas (https://docs.convex.dev/database/schemas#writing-schemas).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: