#type issue

15 messages · Page 1 of 1 (latest)

jaunty gate
#
import { query } from "./_generated/server";
import schema from "./schema.ts";

export const get = query({
  args: {
    slug: schema.tables.properties.validator.fields.slug,
  },
  handler: async (ctx, args) => {
    const property = await ctx.db.query("properties").withIndex(
      "by_slug",
      (q) => q.eq("slug", args.slug),
    ).collect();

    return property;
  },
});
const properties = useQuery(api.getPropertyBySlug, { slug: property });

Diagnostics:

  1. Argument of type '{ get: FunctionReference<"query", "public", { slug: string; }, { _id: Id<"properties">; _creationTime: number; property_title?: string | undefined; description?: string | undefined; min_rental_duration?: string | undefined; ... 26 more ...; price: number; }[], string | undefined>; }' is not assignable to parameter of type 'FunctionReference<"query">'.
    Type '{ get: FunctionReference<"query", "public", { slug: string; }, { _id: Id<"properties">; _creationTime: number; property_title?: string | undefined; description?: string | undefined; min_rental_duration?: string | undefined; ... 26 more ...; price: number; }[], string | undefined>; }' is missing the following properties from type 'FunctionReference<"query">': _type, _visibility, _args, _returnType, _componentPath [2345]
golden siloBOT
#

Hi @jaunty gate. 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 due to a type mismatch between the FunctionReference you're trying to use and the expected type.

In Convex, a FunctionReference is a reference to a registered Convex function. It has several properties including _type, _visibility, _args, _returnType, and _componentPath (source (https://docs.convex.dev/api/modules/server#functionreference)).

In your case, the error message is indicating that the object you're trying to use as a FunctionReference is missing these properties.

The useQuery function expects a FunctionReference as its first argument (source (https://docs.convex.dev/api/classes/browser.ConvexClient#query)). In your code, you're passing api.getPropertyBySlug, which should be a FunctionReference.

However, the error message suggests that api.getPropertyBySlug is an object with a get property, which itself is a FunctionReference. This is not the expected type.

You should pass the FunctionReference directly to useQuery, like so:

const properties = useQuery(api.getPropertyBySlug.get, { slug: property });

This assumes that api.getPropertyBySlug.get is the correct FunctionReference. If getPropertyBySlug is the FunctionReference, you should pass it directly:

const properties = useQuery(api.getPropertyBySlug, { slug: property });

Please ensure that the FunctionReference you're using has the correct type and includes all the necessary properties.

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

#

@jaunty gate Kapa.ai is still learning and improving, please let me know how I did by reacting below.

jaunty gate
#

@golden silo How do I get the type of a table but including relations?

    import type { Doc } from '../../convex/_generated/dataModel';|
let { status, property }: { status: 'for-sale' | 'for-rent'; property: Doc<'properties'> } =
        $props();

    <img
                data-src={property.assets.images.find((img) => img.is_primary)?.asset_url ||
                    property.assets.images[0].asset_url}
                alt="main"
                class="rounded-t-3xl h-full w-full"
            />

Diagnostics:

  1. Property 'assets' does not exist on type '{ _id: Id<"properties">; _creationTime: number; property_title?: string | undefined; description?: string | undefined; min_rental_duration?: string | undefined; available_from?: string | undefined; ... 25 more ...; price: number; }'. [2339]
golden siloBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

The error message you're seeing is because the property object, which is of type Doc<'properties'>, does not have an assets property according to the type definition.

In Convex, the Doc type represents a document in a table, and its properties should match the schema of the table it represents. If the properties table in your schema includes an assets field, then the Doc<'properties'> type should include this field as well.

If you want to include relations in your type, you would need to extend the Doc<'properties'> type to include these relations. However, this would not automatically fetch the related data from the database, you would still need to manually fetch this data in your query or mutation.

Here's an example of how you might define a type that includes relations:

type PropertyWithAssets = Doc<'properties'> & {
  assets: {
    images: Array<{
      is_primary: boolean;
      asset_url: string;
    }>;
  };
};

let { status, property }: { status: 'for-sale' | 'for-rent'; property: PropertyWithAssets } = $props();

In this example, PropertyWithAssets is a new type that includes all the fields from Doc<'properties'> as well as an assets field. The assets field is an object that includes an images array, where each image is an object with is_primary and asset_url fields.

Please note that this is just an example and you would need to adjust it to match your actual data model.

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

#

@jaunty gate Kapa.ai is still learning and improving, please let me know how I did by reacting below.

jaunty gate
#

@golden silo I'm querying the data on the page

const properties = useQuery(api.getProperties.get, { status: status });

and then passing it into the component

{#each properties.data as property}
  <PropertyCard {property} />
{/each}

PropertyCard.svelte
how do i get the type of the queried data inside the component?

golden siloBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

You can get the type of the queried data using the FunctionReturnType helper from Convex. This helper allows you to extract the return type of a Convex function, which in your case would be the return type of the getProperties.get query.

Here's how you can use it:

import type { FunctionReturnType } from 'convex/server';
import { api } from '../../convex/_generated/api';

type PropertiesType = FunctionReturnType<typeof api.getProperties.get>;

In this example, PropertiesType would be the type of the data returned by the getProperties.get query.

Then, you can use this type in your component:

let { property }: { property: PropertiesType } = $props();

This will ensure that the property prop in your PropertyCard component has the correct type.

Please note that you need to adjust the import paths according to your project structure (source (https://discord.com/channels/1019350475847499849/1255545423112835122)).

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

#

@jaunty gate Kapa.ai is still learning and improving, please let me know how I did by reacting below.

jaunty gate
#

@golden silo the above returned the type of array of properties but i'm only going to be passing a single property of that aray into the component

golden siloBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

If you're passing a single property into the component, you can extract the type of a single property from the array using TypeScript's ArrayElement utility type. Here's how you can do it:

import type { FunctionReturnType } from 'convex/server';
import { api } from '../../convex/_generated/api';

type PropertiesArrayType = FunctionReturnType<typeof api.getProperties.get>;

// If PropertiesArrayType is an array of some type T, then ArrayElement<T> will be the type T.
type PropertyType = ArrayElement<PropertiesArrayType>;

In this example, PropertyType would be the type of a single property.

Then, you can use this type in your component:

let { property }: { property: PropertyType } = $props();

This will ensure that the property prop in your PropertyCard component has the correct type.

Please note that you need to adjust the import paths according to your project structure (source (https://discord.com/channels/1019350475847499849/1255545423112835122)).

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