Prisma error with NextJS
Unanswered
Santhosh Prabhakaran posted this in #help-forum
I'm trying to fetch the products from the db using Prisma in my NextJS 13 application. This is the code,
I'm getting this error while fetching like this.
When I fetch separately, It is fetching fine. I don;t know why this is happening. Any help on this would be appreciated.
const ProductsPage = async ({ params }: { params: { storeId: string } }) => {
const products = await prismadb.product.findMany({
where: {
storeId: params.storeId,
},
include: {
category: true,
size: true,
color: true,
},
orderBy: {
createdAt: "desc",
},
});
const formattedProducts: ProductColumns[] = products.map((item) => ({
id: item.id,
name: item.name,
isFeatured: item.isFeatured,
isArchived: item.isArchived,
price: formatter.format(item.price.toNumber()),
category: item.category.name,
size: item.size.name,
color: item.color.value,
createdAt: format(item.createdAt, "MMMM do, yyyy"),
}));
return (
<div className="flex-col">
<div className="flex-1 space-y-4 p-8 pt-6">
<ProductClient items={formattedProducts} />
</div>
</div>
);
};I'm getting this error while fetching like this.
When I fetch separately, It is fetching fine. I don;t know why this is happening. Any help on this would be appreciated.
21 Replies
Cuvier’s Dwarf Caiman
did you import prisma instance ?
and this is server component?
provide complete code...
and this is server component?
provide complete code...
It is a server component.
I'm not getting values when I try to fetch using include method.
Cuvier’s Dwarf Caiman
remove category property into include object and try again..
before format the product object console product object.... and check your terminal
the error is coms from category object.
@Cuvier’s Dwarf Caiman remove category property into include object and try again..
If I remove category, The error comes from size object and wise versa
Cuvier’s Dwarf Caiman
show your prisma model code of products, cateory, size and color
@Santhosh Prabhakaran Click to see attachment
Try using prisma studio or any tool to check if the one of the product's category field is null.
npx prisma studioIs the command
If it is somehow null, then there's your issue. According to your schema it should never be null and if i am correct, if you created the product the using prisma client it should never have even allowed you to create such a product, but who knows.
@Clown `npx prisma studio`
It's not about the category alone. Whatever object I give in "include" method. I'm getting this error
@Santhosh Prabhakaran It's not about the category alone. Whatever object I give in "include" method. I'm getting this error
Yeah but firstly go to prisma studio or phpmyadmin or whatever tool you use to check the current state of your db
And see if its consistent with your schema
Unless i missed something, none of the Product's field should be empty/null
Btw are you creating the products using the client or manually using a tool/manually issuing commands?
Also if you are using the client, you MUST connect/create a connection of each relation field, check the prisma docs for more Info. Also the the model you are creating a reference to MUST be valid and exist.
Also if you are using the client, you MUST connect/create a connection of each relation field, check the prisma docs for more Info. Also the the model you are creating a reference to MUST be valid and exist.