I'm encountering a problem with using the prisma .findUnique() function. Even though my code doesn't display any compilation errors, attempting to access a product page triggers a runtime error.
PrismaClientValidationError:
Invalid `prisma.product.findUnique()` invocation:
{
where: {
id: "6"
~~~
}
}
Argument `id`: Invalid value provided. Expected Int, provided String.
Despite using the product id as an int, it seems that .findUnique() is interpreting it as a string. Can anyone suggest a solution to make .findUnique() function correctly?
Here is my schema.prisma file displaying the schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Product {
id Int @id @default(autoincrement())
name String @db.VarChar(127)
description String @db.VarChar(511)
imageUrl String @db.VarChar(255)
price Int @db.SmallInt
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("products")
}
This is the section where I attempt to find an item by its id:
interface ProductPageProps {
params: {
id: number;
};
}
const getProduct = cache(async (id: number) => {
const product = await prisma.product.findUnique({ where: { id } });
if (!product) notFound();
return product;
});
export async function generateMetadata({
params: { id },
}: ProductPageProps): Promise<Metadata> {
const product = await getProduct(id);
return {
title: product.name + "Project",
description: product.description,
openGraph: {
images: [{ url: product.imageUrl }],
},
};
}
export default async function ProductPage({
params: { id },
}: ProductPageProps) {
const product = await getProduct(id);
return (
<div className="flex flex-col gap-4 lg:flex-row lg:items-center">
<Image
src={product.imageUrl}
alt={product.name}
width={400}
height={400}
className="rounded-lg"
priority
/>
<div>
<h1 className="text-5xl font-bold">{product.name}</h1>
<PriceTag price={product.price} className="mt-4" />
<p className="py-6">{product.description}</p>
</div>
</div>
);
}
I have attempted converting the number to a string and passing it into .findUnique() just to test if it needs to be a string:
const stringId = id.toString();
const product = await prisma.product.findUnique({ where: { stringId } });
However, this gives me an error:
Object literal may only specify known properties, and 'stringId' does not exist in type
'ProductWhereUniqueInput'.ts(2353)
index.d.ts(1548, 5): The expected type comes from property 'where' which is declared
here on type '{ select?: ProductSelect<DefaultArgs> | null | undefined; where: ProductWhereUniqueInput; }'
It appears to be a type error, and I'm unsure how to resolve it. Any suggestions on how to tackle this issue would be appreciated.