I don’t know why it’s not working in Next.js, but it still works normally in another templates.
let subTotal = 0
if (selectedProducts?.length) {
for (let id of selectedProducts) {
const price = products.find(product => product.id === id).price
subTotal += price
}
}
Here my GitHub code: e-commerce
I’ve tried everything that i know, please i need help, tysm.
2
Answers
Presumably
products.find(product => product.id === id)
isn’t returning an object in at least one iteration of the loop. Maybe include a little defensive coding to ensure that price is available.For example:
Additional documentation
Inside
useEffect
set the loading state insidethen
block to make sure you set the loading state right after async data fetchingthen calculate the subTotal after
loading==false