adrianhajdin / ecommerce_sanity_stripe

Modern Full Stack ECommerce Application with Stripe
https://jsmastery.pro
2.21k stars 674 forks source link

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

Open Awaisp-irfan opened 2 years ago

Awaisp-irfan commented 2 years ago

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

Bitrate2001 commented 2 years ago

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

Awaisp-irfan commented 2 years ago

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

F00r3y3s commented 2 years ago

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"

Awaisp-irfan commented 2 years ago

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

RobertoBenedit commented 2 years ago

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.

shivam-jha2712 commented 2 years ago

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

Zkirvalidze commented 2 years ago

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; } });

bluevoxInc commented 1 year ago

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!