简体   繁体   English

将对象推送到嵌套的 Typescript 数组中,得到错误:无法读取未定义的属性“推送”

[英]Push object into nested Typescript array, get error: Cannot read property 'push' of undefined

I've been looking around SO for a bit trying to solve this but my code seems to go a little beyond a standard "push object into array."我一直在四处寻找试图解决这个问题,但我的代码似乎有点超出标准的“将对象推入数组”。

I have an interface that types a "Year" property with another interface as an array:我有一个接口,它使用另一个接口作为数组键入“Year”属性:

   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 a certain point I'm going to pass some formGroup data into the IYear typed property:在某个时候,我将一些 formGroup 数据传递到 IYear 类型属性中:

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 problem is, when I try to pass the 'year' property inside of createUser into the 'this.user.Year' array, I get the error问题是,当我尝试将 createUser 中的 'year' 属性传递到 'this.user.Year' 数组时,出现错误

Cannot read property 'push' of undefined无法读取未定义的属性“推送”

I've tried explicitly passing in the values by key:我已经尝试通过键显式传递值:

this.user.Year['CalendarYear'] = year.CalendarYear;

But that didn't work.但这没有用。 Can anyone point out my mistake here?有人能在这里指出我的错误吗?

The Year array in the user Object should be initialized to empty before pushing objects into it.在将对象推入其中之前,用户对象中的 Year 数组应初始化为空。

this.user.Year = [];

and then,进而,

this.user.Year.push(year);

The reason you get the error is because you have to first initialize an array , and then you can push() values to it.出现错误的原因是您必须首先初始化一个 array ,然后才能向其push()值。

  • Either initialize a new IUser with a year = [{your year}] , and then you can push more values to it.要么用year = [{your year}]初始化一个新的IUser然后你可以向它推送更多的值。
  • Or initialize a new IUser object with an empty array year, and then you can push values to it.或者用一个空数组 year 初始化一个新的IUser对象,然后你可以向它推送值。

you have initialize your array before use it.您已在使用之前初始化您的数组。

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=[];
    this.user.Year.push(year);    
    return this.user;
  }

您需要确保Year存在于用户对象上,并且CalendarYear存在于Year对象上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM