I am generating a store project, but I have the following problem when generating the [product] page with the slug, it shows me an Error: Cannot read properties of null (reading 'name')
app\product[productId]\page.tsx
import { getProduct } from "@/sanity/sanity-utils";
type Props = {
params: { product: string }
}
export default async function Product({ params }: Props) {
const slug = params.product;
const product = await getProduct(slug);
return <div>{product.name}</div>
}
sanity\sanity-utils.ts
export async function getProduct(slug: string): Promise<Product> {
return createClient(clientConfig).fetch(
groq`*[_type == "project" && slug.current == "${slug}"][0]{
_id,
createdAt,
name,
"slug": slug.current,
brand,
"image": image[].asset->url,
pack,
sku,
sizes,
description,
"category": category->name,
"subcategory": subcategory->name
}`,
`{slug}`
)
}
on the sanity-utils page, if I remove the `` from the {slug} it shows me the error Error: Unable to parse value of "$slug=undefined". Please quote string values. but it didn't solve anything, it just gave me the error described above.
Please help, thank you all very much in advance!!!
Replace the product param in the props type with productId, to bring it in line with your defined route.