-1

When I call my function is throwing an error. I'm trying to insert my items via a forEach in angular. Below is the code which I tried.

Console Error Logs

Ts code

sItems: SaleProduct[];

btnSale() {

let tsale = new SaleProduct();

let tcustomerId = this.postForm.get('customer').value;

console.log(this.items);

this.items.forEach(obj => {
  tsale.CustomerId = tcustomerId;
  tsale.ProductId = obj.Id;
  tsale.CostPrice = obj.CostPrice;
  tsale.SellingPrice = obj.SellingPrice;

  console.log(tsale);
  console.log(this.sItems);

  this.sItems.push(tsale);
  });
}
2

1 Answer 1

2
sItems: SaleProduct[];

This declares the object but doesn't initialize it and hence you get the error as the object is undefined.

Initialize it by the following code:

sItems: SaleProduct[] = [];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.