For a while now, I've been searching on SO trying to find a solution to my issue. It seems that my code isn't as simple as just "push object into array."
In my interface, I have a "Year" property typed with another interface as an array:
export interface IUser {
Nickname: string;
FirstName: string;
LastName: string;
Email: string;
EmergencyContact: string;
EmergencyNumber: string;
Year: Array<IYear>;
IsTeacher: boolean;
IsAdmin: boolean;
Uid: string;
}
export interface IYear {
CalendarYear: string;
PassType: string;
UserItinerary: IItinerary;
WaiverStatus: boolean;
Guests: Array<IGuest>;
SignupDate: string;
}
At some point, I need to pass formGroup data into the IYear typed property:
user: IUser = <IUser>{};
createUser(
firstName: string,
lastName: string,
email: string,
uid: string,
isTeacher: boolean,
passType: string) {
const year: IYear = {
CalendarYear: this.calendarYear,
PassType: passType,
UserItinerary: {} as IItinerary,
WaiverStatus: false,
Guests: [],
SignupDate: this.dateTime
}
this.user.FirstName = firstName;
this.user.LastName = lastName;
this.user.Email = email;
this.user.Uid = uid;
this.user.IsTeacher = isTeacher;
this.user.IsAdmin = false;
this.user.Year.push(year);
return this.user;
}
The issue arises when I try to push the 'year' property from createUser into the 'this.user.Year' array, resulting in the error
Cannot read property 'push' of undefined
I attempted to pass the values explicitly by key:
this.user.Year['CalendarYear'] = year.CalendarYear;
However, that approach didn't work. Can anyone help identify what mistake I made here?