2

I've got a computed 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.

kabugh
  • 305
  • 1
  • 9
  • 30
  • Are these computed variables? – Bharathvaj Ganesan Feb 28 '18 at 20:47
  • You expect `this.discountValue.discount` to call your `discountValue()` method? – Crescent Fresh Feb 28 '18 at 20:47
  • 1
    `discountValue.discount` makes no sense (neither does setting `discountActive = true` then immediately throwing it away). What are you trying to do? – Dave Newton Feb 28 '18 at 20:47
  • 2
    `discountValue()` seems to assume that `cartTotal()` returns a price without the discount subtracted from it, but then in `cartTotal()` you want to subtract the discount from it. There is a logical error here. You must make up your mind: does `cartTotal()` include the discount or not? – trincot Feb 28 '18 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 Feb 28 '18 at 20:54
  • The `cartTotal()` doesn't include the discount, but when the user types in a suitable input the correct discount code, I would like to include the discount to the main `cartTotal()`. – kabugh Feb 28 '18 at 20:55
  • Why `discountValue` needs to be a seperate computed value? You can do what you want in `cartTotal` function only. – Alex Michailidis Feb 28 '18 at 21:13
  • Yeah, you're right but still doesn't work for me :/ ` 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;` – kabugh Feb 28 '18 at 21:19
  • Don't add long code to comments, it's difficult to read, instead, update your question with things you're trying – StudioTime Feb 28 '18 at 21:22
  • Update the question please, and what is the error now? It shouldn't be maximum call stack... – Alex Michailidis Feb 28 '18 at 21:23

1 Answers1

4

You're getting that error because you have two computed 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 computed properties:

computed: {
  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;
  },
}
thanksd
  • 54,176
  • 22
  • 157
  • 150