<div className={styles.Panier}>
<p onClick={()=>Quantity>0?NewItem(data,Quantity):console.log("Quantity must be more
than 0")}>Add To Cart</p>
</div>
Upon clicking the above div, it triggers the NewItem
function as shown below:
let NewItem = async (data,Qte) => {
const response = await fetch('/api/UpdateCart',{
method: "POST",
body: JSON.stringify(
{
Qte,
data,
cartId: Cookies.get('cart')
}
),
headers:{
'Content-Type': 'application/json; charset=utf8'
},
});
console.log(response);
};
The expected flow is for the request to go to the '/api/UpdateCart' API (which uses Prisma):
import { PrismaClient } from "@prisma/client";
export default async function handler(req, res)
{
let prisma = new PrismaClient();
let Id = req.body.Id;
let Qte = req.body.Qte;
let cartId = req.body.cartId;
let newItem = await prisma.item.create({
data: {
ProductId: Id,
Quantity: Qte,
CartId: cartId
}
});
return res.status(200).json(newItem);
}
An issue arises where accessing the endpoint through Postman works fine, but using the NewItem
function mentioned earlier results in a 500 server error displayed like so:
POST http://localhost:3000/api/UpdateCart 500 (Internal Server Error)
Response {type: 'basic', url: 'http://localhost:3000/api/UpdateCart', redirected: false, status: 500, ok: false, …}
body
:
(...)
bodyUsed
:
false
headers
:
Headers {}
ok
:
false
redirected
:
false
status
:
500
statusText
:
"Internal Server Error"
type
:
"basic"
url
:
"http://localhost:3000/api/UpdateCart"
[[Prototype]]
:
Response
Your assistance on resolving this issue would be greatly appreciated. Thank you for taking the time to help.