I am encountering a common issue while working with TypeScript. The error message I am receiving is:
ERROR TypeError: Cannot read property 'push' of undefined
In my code, I have defined a model called EmailModel
:
export class EmailModel {
public name: String;
public lastname: String;
public address: String;
public company: String;
public zipcode: number;
public city: String;
public phonenumber: number;
public email: String;
public product: Array<ProductModelOrder>=[];
constructor(name: String, lastname: String, address: String, company: String, zipcode: number, city: String, phonenumber: number, email: String,product: Array<ProductModelOrder>=[]) {
this.name = name;
this.lastname = lastname;
this.address = address;
this.company = company;
this.zipcode = zipcode;
this.city = city;
this.phonenumber = phonenumber;
this.email = email;
this.product = product;
}
}
Furthermore, I have defined a product
array in the model:
export class ProductModelOrder {
public name: String;
public number: number;
public pricePerProduct:number;
public price:number;
}
My intention is to assign values from productOrder
to emailModel
using the following logic:
for (let prod of this.productCarts){
this.productOrder.name = prod.product_name;
this.productOrder.number = prod.numberOfProduct;
this.productOrder.pricePerProduct = prod.product_price;
this.productOrder.price = this.priceForAllProducts;
this.emailModel.product.push(this.productOrder);
}
Despite my efforts, I am encountering an error.