I have a class Article which has property of type Image and looks like this:
export class Article {
public image:Image;
public images: Image[];
}
If I comment this.article.image = new Image(); like this:
constructor()
{
this.article = new Article();
//this.article.image = new Image();
}
And If I try to use an this.image later in code, like:
this.article.image.fileName = file.name;
I will get following error:
ERROR TypeError: Cannot set property '..' of undefined
But I realized in case when I try to add something to an array for example:
this.article.images.push(something);
I am not getting error! And I'm wondering why I don't get error when I use array (even if I did not said this.article.images = new Images[].. , and why I get error when I'm using simple object/not array..
this.article.imageisundefined, You havent assigned any value to it, You should create newImageobjectand assign it tothis.article.imagebefore setting file name like this ->this.article.image.fileName = file.name;