I am encountering an issue while attempting to add an array of Funcionarios
objects into a Equipa
object. When trying to use the push
method to add a new Funcionarios
object, I receive the error message
TypeError: Cannot read property 'push' of undefined
. Despite confirming that all variables are initialized and reviewing the code multiple times, this error persists.
export class Funcionarios {
id : Number;
codFunc: Number;
nomeFunc: string;
constructor(codFunc: Number, nomeFunc: string) {
this.codFunc = codFunc;
this.nomeFunc = nomeFunc;
}
}
export class Equipa {
id : Number;
codEquipa: Number;
nomeEquipa: string;
ocorFuncs: Funcionarios[] = [];
constructor(id : Number, codEquipa: Number, nomeEquipa: string, funcs: Funcionarios[]) {
this.id = id;
this.codEquipa = codEquipa;
this.nomeEquipa = nomeEquipa;
this.ocorFuncs = funcs;
}
}
export class TestComponent implements OnInit {
equipa: Equipa = new Equipa(null, null, null, null);
ngOnInit() {
this.equipa.ocorFuncs.push(new Funcionarios(1, "qwe"));
this.equipa.ocorFuncs.push(new Funcionarios(2, "asd"));
this.equipa.ocorFuncs.push(new Funcionarios(3, "zxc"));
}
}