The code is below. It is not looping through all of the items in the array and I think it's because async calls and forEach loops aren't the best solution for this.
async function getSubtotal(itemArray) { var total = 0.0 try { itemArray.forEach(async (itemObject) => { let id = itemObject['id'] console.log('itemId', id) const itemDoc = await db.collection('Menu').doc(id).get() const data = itemDoc.data() total += data.price console.log('total after item is appended', total) const selectedOptionIds = itemObject['selectedOptions'] selectedOptionIds.forEach((optionId) => { const optionObject = data.allOptions optionObject.forEach((option) => { console.log('selectedOptionId', optionId) if (option.id === optionId) { total += option.price console.log('total after option is appended', total) } }) }) }) console.log('finalTotal', total) return total * 100 // convert to cents } catch (error) { console.error(error) return { error: error } } }
2.1m questions
2.1m answers
60 comments
57.0k users