我想計算購物籃的總金額。我有ShoppingCartItem和產品表。我有婁代碼Angular2嵌套可觀察車總數,但返回值未定義
cartTotals(qty = 0, total = 0) {
return this.af.database.list('ShoppingCartItem')
.map(carts => {
carts.map(cart => {
this.af.database.object(`Product/${cart.productId}`)
.subscribe(d => {
cart.product = d;
});
return cart;
});
carts.forEach(cartItem => {
qty += cartItem.quantity;
total += cartItem.quantity * cartItem.product.price;
// console.log(cartItem);
});
return {qty, total};
});
}
返回數量價值的作品,但總值返回undefined
'total'不能因爲它的默認值是數字0 –
cartItem.product.price回報不確定,因爲總價值迴歸未定義或錯誤 –
可能的複製[我要計算的總和是'undefined'購物車在Firebase與Angularfire2](http://stackoverflow.com/questions/40833704/i-want-to-calculate-the-sum-of-the-shoppingcart-at-firebase-with-angularfire2) – STEEL