0

I have model EmailModel class model :

    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: ProductModelOrder[] = [];



    constructor(name: String, lastname: String, address: String, company: String, zipcode: number, city: String, phonenumber: number, email: String,product: 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;
    }
}

And I created the variable emailModel of my class EmailModel. This is my var: emailModel =< EmailModel>{};

I get the undefined error when I using the this.emailModel.product, but when I using the this.emailModel.name or other properties everting is well.

1
  • 2
    Like this : emailModel =< EmailModel>{}; Commented Sep 24, 2019 at 16:11

3 Answers 3

1

you need to initilize array to be as empty array like this in order to use like normal property of object -

public product: ProductModelOrder[] = [];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this sole problem.
Could you please see my updated EmailModel class, I have the same error and it is very strange
for me working fine, see here stackblitz.com/edit/angular-adug2r, can you reproduce your issue here?
1

The product attribute is an array of "ProductModelOrder" and should also be initialized

1 Comment

Thanks for the help.
1

One thing is your class definition, other thing is instantiating your object. Your class definition looks alright.

Now lets try to instantiate:

let products: ProductModelOrder[] = []; //Creates an instance for the array of products
let product: ProductModelOrder = new ProductModelOrder(...); //Creates an instance for a product
product.someproperty = "somevalue"; //Sets value to property
products.push(product); //Adds the product to the list

let emailModel = new EmailModel(..., products); //Instantiates the main object, passing the instantiated array

console.log(emailModel.products[0].someproperty);//Logs the value of the property "someproperty", in this example it should print "somevalue".

Please note that I have renamed the property EmailModel.product to EmailModel.products because it can contain many products.

1 Comment

Thanks for this example is useful.

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.