dannyeloD
Convex Community2y ago
6 replies
dannyelo

Patch mutation function is giving me a typescript error

I'm trying to insert a document to a table in various steps. When I insert the first two pieces of data, the record is created and I push the id as searchParams so I can have access in the next form component.
And as user keep filling forms, the document needs to receive the remaining data.

This is the schema
  export default defineSchema({
  listings: defineTable({
    userSubjectId: v.string(),
    title: v.string(),
    department: v.string(),
    manufacturer: v.optional(v.string()),
    brand: v.optional(v.string()),
    model: v.optional(v.string()),
  }),
});

I first insert data into the required fields

This is the function I'm trying to implement now.
export const saveListingMainSpecs = mutation({
  args: {
    listingId: v.id("listings"),
    manufacturer: v.string(),
    brand: v.string(),
    model: v.string(),
  },
  handler: async (ctx, args) => {
    const { listingId } = args;
    await ctx.db.patch(listingId, {
      manufacturer: args.manufacturer,
      brand: args.brand,
      model: args.model,
    });
  },
});


This is the typescript error I'm getting in the frontend
  function onSubmit(data) {
    const listingId = searchParams.get("listingId");
    saveListingMainSpecs({
      listingId,
      manufacturer: data.manufacturer,
      brand: data.brand,
      model: data.model,
    });
  }


Error is at listingId.
Type 'string | null' is not assignable to type 'Id<"listings">'.
  Type 'null' is not assignable to type 'Id<"listings">'.
    Type 'null' is not assignable to type 'string'.ts(2322)
type_utils.d.ts(11, 97): The expected type comes from property 'listingId' which is declared here on type '{ manufacturer: string; brand: string; model: string; listingId: Id<"listings">; }'


Any help is appreciated!
image.png
Was this page helpful?