我想计算购物篮的总金额。我有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