ecommerce_sanity_stripe icon indicating copy to clipboard operation
ecommerce_sanity_stripe copied to clipboard

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

Open Awaisp-irfan opened this issue 2 years ago • 8 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

Awaisp-irfan avatar Jul 26 '22 11:07 Awaisp-irfan

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

Bitrate2001 avatar Jul 31 '22 14:07 Bitrate2001

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

Awaisp-irfan avatar Aug 03 '22 15:08 Awaisp-irfan

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"

F00r3y3s avatar Aug 06 '22 13:08 F00r3y3s

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

Awaisp-irfan avatar Aug 10 '22 07:08 Awaisp-irfan

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.

RobertoBenedit avatar Aug 11 '22 17:08 RobertoBenedit

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

shivam-jha2712 avatar Sep 16 '22 22:09 shivam-jha2712

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

Zkirvalidze avatar Sep 29 '22 14:09 Zkirvalidze

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!

bluevoxInc avatar Jan 27 '23 20:01 bluevoxInc