adrianhajdin / ecommerce_sanity_stripe

Modern Full Stack ECommerce Application with Stripe

Home Page:https://jsmastery.pro

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot read properties of undefined (reading '_id')

Awaisp-irfan opened this issue · comments

I'm facing an error, this error is actually in this repo.
when I click the "Add to cart" button and add a product to the cart it works fine,
After that when I select a second product from the suggestions and add that product to the cart after increasing the quantity of that product it works as expected and I can see two products in the cart list.

The error occurs when the second product is in the cart with quantity 3 or 4 and I decrease the quantity and add it to the cart again
I linked a URL of a video that will help you understand the problem
https://www.loom.com/share/19b3420698354d3eb12816fe68357a31

Screenshot (3)
28bec69879e5475eac4d750d48dc3b31

I found the same issue, can someone provide the fix for this error ?

@adrianhajdin, hey man could you look at this issue for us

The fixes for these issues are already provided in the issue section, the only thing I couldn't find is how to reset the quantity back to 1 once we move on to another product

product A = quantity 4

when click on another product

the quantity is still 4 and doesn't revert back to it's initial state which is "1"

hi Foor3y3s ,
Thanks for your help,
Could you tell me which issue it was

The error "Property does not exist on type 'never'" occurs when we try to access a property on a value of type never or when TypeScript gets confused when analyzing our code. To solve the error, use square brackets to access the property. e.g.

const onAdd = (product, quantity) => {
const checkProductInCart = cartItems.find((item) => item['_id'] === product._id);

In short, do obj['myProperty'], instead of obj.myProperty.

I am also facing a similar kind of error: if any of you could help me with this I am putting the video link https://www.loom.com/share/701a8b0c08144b34b4876296aa9a0a0c

You just need to return item , in this particular part of onAdd function. as shown below

if (checkProductInCart) {
const updatedCartItems = cartItems.map((item) => {
if (item._id === product._id)
return {
...item,
quantity: item.quantity + quantity,
};
{
return item;
}
});

You just need to return item , in this particular part of onAdd function. as shown below

if (checkProductInCart) { const updatedCartItems = cartItems.map((item) => { if (item._id === product._id) return { ...item, quantity: item.quantity + quantity, }; { return item; } });

This worked for me, thanks!