need help on a server action on my Next.js applicastion, giving me a type error and can’t pinpoint the reason, it appears when there are 3 values on the db.insert
for the orderProduct and it always errors out the first value whatever it field it is.
create-order.tsx
products.map(async ({ productId, quantity, variantId }) => {
await db.insert(orderProduct).values({
productVariantId: variantId,
productId: productId,
quantity,
orderId: order[0].id,
})
})
schema
export const orderProduct = pgTable('order_product', {
id: serial('id').primaryKey(),
quantity: integer('quantity').notNull(),
productVariantId: serial('productVariantId')
.notNull()
.references(() => productVariants.id, { onDelete: 'cascade' }),
productId: serial('productId')
.notNull()
.references(() => products.id, { onDelete: 'cascade' }),
orderId: serial('orderId')
.notNull()
.references(() => orders.id, { onDelete: 'cascade' }),
})
Types
import * as z from 'zod'
export const createOrderSchema = z.object({
total: z.number(),
status: z.string(),
paymentIntentId: z.string(),
products: z.array(
z.object({
quantity: z.number(),
productId: z.string(),
variantId: z.string(),
}),
),
})
Error message
No overload matches this call.
Overload 2 of 2, '(values: { quantity: number | SQL<unknown> | Placeholder<string, any>; id?: number | SQL<unknown> | Placeholder<string, any> | undefined; productId?: number | SQL<...> | Placeholder<...> | undefined; productVariantId?: number | ... 2 more ... | undefined; orderId?: number | ... 2 more ... | undefined; }[]): PgInsertBase<...>', gave the following error.
Object literal may only specify known properties, and 'productVariantId' does not exist in type '{ quantity: number | SQL<unknown> | Placeholder<string, any>; id?: number | SQL<unknown> | Placeholder<string, any> | undefined; productId?: number | SQL<...> | Placeholder<...> | undefined; productVariantId?: number | ... 2 more ... | undefined; orderId?: number | ... 2 more ... | undefined; }[]'.ts(2769)
Here’s the error, it will be the same if i move around the values of my db.insert
2
Answers
Found the answer, on my types it should be
z.number()
instead ofz.string()
for theproductId
andvariantId
Modular Design:
a single source of truth for common fields.
use cases.
Schema Naming Conventions:
structures (e.g., DbProductSchema).
specific operation schemas.
This will assist you in managing any type of project. I’ve included an example for a date. In the same way, you can add product schema.