I've got a puted method that allows me to count a total price of my products and discount value and would like to get the value of: total - discount.
cartTotal() {
var total = 0;
var discount = Math.round((0.1 * this.cartTotal) * 100) / 100;
this.cart.items.forEach(function(item) {
total += item.quantity * item.product.price;
});
total -= discount;
return total;
}
Doens't work for me and I get that Maximum call stack size exceeded
error.
I've got a puted method that allows me to count a total price of my products and discount value and would like to get the value of: total - discount.
cartTotal() {
var total = 0;
var discount = Math.round((0.1 * this.cartTotal) * 100) / 100;
this.cart.items.forEach(function(item) {
total += item.quantity * item.product.price;
});
total -= discount;
return total;
}
Doens't work for me and I get that Maximum call stack size exceeded
error.
- Are these puted variables? – Bharathvaj Ganesan Commented Feb 28, 2018 at 20:47
-
You expect
this.discountValue.discount
to call yourdiscountValue()
method? – Crescent Fresh Commented Feb 28, 2018 at 20:47 -
1
discountValue.discount
makes no sense (neither does settingdiscountActive = true
then immediately throwing it away). What are you trying to do? – Dave Newton Commented Feb 28, 2018 at 20:47 -
2
discountValue()
seems to assume thatcartTotal()
returns a price without the discount subtracted from it, but then incartTotal()
you want to subtract the discount from it. There is a logical error here. You must make up your mind: doescartTotal()
include the discount or not? – trincot Commented Feb 28, 2018 at 20:52 -
I've got some products that have prices and after typing a correct code to give discount to the price of that product. So I calculate the value of the discount and would like it to be 0,1 of my total price: ` var discount = Math.round((0.1 * this.cartTotal) * 100) / 100` - that's correct. Then would like to subtract that value from my main value
total
. – kabugh Commented Feb 28, 2018 at 20:54
1 Answer
Reset to default 5You're getting that error because you have two puted properties that reference each others' value. Whenever you do that, you create a cyclical dependency which will generate a "Maximum call stack size exceeded" error.
You really have three values you're dealing with 1) a sum of all values in the cart, 2) a discount amount, and 3) a total value which is the sum minus the discount.
You should have three puted properties:
puted: {
cartSum() {
return this.cart.items.reduce((n, i) => n += i.quantity * i.product.price, 0);
},
discountValue() {
return Math.round((0.1 * this.cartSum) * 100) / 100;
},
cartTotal() {
return this.cartSum - this.discountValue;
},
}