Home > Net >  Why can I not push into this array?
Why can I not push into this array?

Time:09-14

I'm getting a value and I want to push the received value in the array. I know the value is deliverd by the service. I have checked. If I would change my code to console.log(product), every product is logged by the console as it is supposed to be. But when I push it to the array in which I want, I get error: "orderinfo.component.ts:26 ERROR TypeError: Cannot read properties of undefined (reading 'push')".

Why can I not push in this array?

export class OrderinfoComponent implements OnInit {
  order!:Order;
  orderdProduct!:Salesproduct[];


  constructor(private router :Router, private salesproductservice: SalesproductService) {
    this.order = router.getCurrentNavigation()?.extras.state as Order;
   }

  ngOnInit(): void {
      this.order.orderlineSet.forEach(orderline => 
          this.salesproductservice.getProductByArtikelnr(orderline.artikelcode)
                .subscribe(product =>
                  this.orderdProduct.push(product)));
  }


}

CodePudding user response:

Your orderdProduct variable hasn't been initialized. Try

orderdProduct!:Salesproduct[] = [];

CodePudding user response:

Your orderdProduct variable hasn't been initialized. Try:

orderdProduct!:Salesproduct[] = []:
  • Related