Pipe isn't returning the right values

54 views
Skip to first unread message

Maureen Moore

unread,
Jul 22, 2020, 10:05:50 AM7/22/20
to Angular and AngularJS discussion
I have a pipe that is supposed to add the sub totals together to return the grand total but it just returns the sub total. It returns sub_total = product_price * quantity; and not the sum of the sub totals.

This is my pipe:

@Pipe({
  name
: 'grandtotal'
})


export class GrandTotalPipe implements PipeTransform {
 
  transform
(product_price: number, quantity: number) {
 
var totals = [];
   
var sub_total;
    sub_total
= product_price * quantity;
   
    totals
.push(sub_total);
var i;
var grand_total = 0;
for (i = 0; i < totals.length; i++) {
 grand_total
+= totals[i];
}
return grand_total;
 
}
}



This is how I call the pipe in the HTML:

<div>Grand Total {{ product_price | grandtotal:  quantity : sub_total : grand_total : totals  }}</div>



When I remove "var totals = [];", I get "cannot read property push of undefined." When I just use "var totals;", I get "Subsequent variable declarations must have the same type.  Variable 'totals' must be of type 'any[]', but here has type 'any'."

Maureen Moore

unread,
Jul 22, 2020, 7:18:39 PM7/22/20
to Angular and AngularJS discussion
I've tried putting "var totals = [];" everywhere. I've tried putting it before the transform function and I've tried putting it in the component. It can't go anywhere except where it is now but that's leading to my issue so that's no good. 

diaa hamad

unread,
Jul 23, 2020, 2:39:19 AM7/23/20
to ang...@googlegroups.com
First issue you are pass 4 arguments to this pipe from html and you set the transform function to recive just 2 arguments

On Thu, 23 Jul 2020, 01:19 Maureen Moore, <maka...@gmail.com> wrote:
I've tried putting "var totals = [];" everywhere. I've tried putting it before the transform function and I've tried putting it in the component. It can't go anywhere except where it is now but that's leading to my issue so that's no good. 

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/bdb0a2ae-d6df-425f-9794-b5f3a864854eo%40googlegroups.com.

Maureen Moore

unread,
Jul 23, 2020, 7:06:16 AM7/23/20
to Angular and AngularJS discussion
I used to be getting 4 arguments. I just forgot to change it for the post.

Maureen Moore

unread,
Jul 24, 2020, 7:51:16 AM7/24/20
to Angular and AngularJS discussion
I tried making a getter like the following but it produces a blank page:

get grandTotal() {
var i;
var sub_total;
var grand_total;
sub_total
= this.product_price * this.quantity;
this.totals.push(sub_total);
for (i = 0; i < this.totals.length; i++) {
 grand_total
+= this.totals[i];
}
return grand_total;
}



Maureen Moore

unread,
Jul 24, 2020, 8:01:10 AM7/24/20
to Angular and AngularJS discussion
I initialize totals outside of the getter function as totals: any[]; and I call the getter in the HTML with <div>Grand Total {{ grandTotal }}</div>

Maureen Moore

unread,
Jul 24, 2020, 8:23:48 AM7/24/20
to Angular and AngularJS discussion
When I put var totals = []; into my getter it returns a value but the totals array is set to null each time so it returns the wrong value.


Maureen Moore

unread,
Jul 24, 2020, 11:53:50 AM7/24/20
to Angular and AngularJS discussion
My getter throws the following error: ERROR TypeError: Cannot read property 'push' of undefined

Maureen Moore

unread,
Jul 24, 2020, 12:11:55 PM7/24/20
to Angular and AngularJS discussion
When I use totals : number[ ] = [ ]; instead of totals: any[];, the typeerror goes away but it displays NaN for the grand total.
Reply all
Reply to author
Forward
0 new messages