Currently, I am delving into the world of NextJS and experimenting with sending requests to an API while passing a parameter that is then used by Prisma to query the database.
In order to achieve this, I've created a new file located at /api/posts/[slug].ts
and initiated a request like so: /api/posts/this-is-my-slug
.
To extract the slug parameter from the URL, I utilize the following code snippet:
const { slug } = req.query;
Subsequently, my intention is to pass the extracted slug value to the 'where' clause in the Prisma query as shown below:
const article = await prismaClient.posts.findFirst({
where: {
slug: slug
}
})
However, upon implementation, I encounter the subsequent error message:
TS2322: Type 'string | string[]' is not assignable to type 'string | StringFilter'. Type 'string[]' is not assignable to type 'string | StringFilter'.
Provided below is the schema model for the posts table within my Prisma setup:
model posts {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
title String @db.VarChar(255)
content String @db.MediumText
slug String @db.VarChar(255)
published Boolean @default(false)
author users @relation(fields: [authorId], references: [userId])
authorId Int
}
The root cause of this error eludes me at the moment. Interestingly, no errors are displayed in the IDE when I hardcode the slug string, yet utilizing the variable from req.query seems to trigger some discontent.