Angular: error in model with object listing

0

I have these 2 templates, and the book has a list of language objects.

Model book
import { Language} from "./language";
export class Book {
    id: number;
    name: string;
    language: Array<Language>;
}

Model language
export class Language{
    id: number;
    name: string;
}

Quando tento criar o objeto livro e adicionar uma linguagem na sua listagem de linguagens, tenho erro de undefined:

myBook: Book();
myLanguage: Language();

hello() {
    this.myLanguage = new Language();
    this.myLanguage.id = 1;
    this.myLanguage.name = "portuguese";

    this.myBook = new Book();
    this.myBook.id = 1;
    this.myBook.name = "one book";
    this.myBook.language.push(this.myLanguage); //error - undefined
}

Would you need a different kind of declaration for this listing?

Thank you.

    
asked by anonymous 27.12.2018 / 18:09

1 answer

2

Before push , associate an empty array with language :

this.myBook.language = [];
this.myBook.language.push(this.myLanguage);
    
27.12.2018 / 18:20