Checkout fails when there is no coupon
TypeError: Cannot read properties of undefined (reading 'percent')
- Service
- basket
- Version
3b7e0c1- Confidence
- 9 out of 10
Likely cause
applyDiscount assumes that every basket has a coupon.
src/discount.js
9 function applyDiscount(basket) {10 const percent = basket.coupon.percent;11 return basket.total * (1 - percent / 100);12 }
Suggested fix
Treat a missing coupon as no discount.
- const percent = basket.coupon.percent;+ const percent = basket.coupon?.percent ?? 0;
Evidence
TypeError: Cannot read properties of undefined (reading 'percent') at applyDiscount (/app/src/discount.js:10:33) at checkout (/app/src/routes/checkout.js:24:17)
Filed by Nattevakt. Review the suggestion before you apply it.