Angular: error in model with object listing

Asked

Viewed 50 times

0

I have these 2 models, and the book has a list of language-like 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 need some different kind of statement for that listing?

Thank you.

1 answer

3


Before the push, associate an empty array to language:

this.myBook.language = [];
this.myBook.language.push(this.myLanguage);
  • That was it, thank you !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.