0

This is my model:

export interface IUser{
    id: number;
    firstName: string;
    lastName: string;
    accounts?:
    {
        userID: number;
        accType: AccType;
        badges: Badge[];
        accountStatus: AccStatus;
    }[];
}

In my user.service.ts, I'm initializing the user before using it in the HTTP services. But can't seem to initialize accounts correctly, since I can't access its values.

private initializeUser(): IUser{
    return {
      id:0,
      firstName: null as any,
      lastName: null as any,
      accounts:{
         userID: null as any,
        accType: null as any,
        badges: null as any,
        accountStatus: null as any
        }
    };
  }

Also, I think since badges are an array, I'm initializing those incorrectly as well.

2 Answers 2

1

I would suggest to make ids facultative. It will allow you to know if an user is new, or not.

export interface IUser {
    id?: number;
    firstName: string;
    lastName: string;
    accounts?:
    {
        userID?: number;
        accType: AccType | null;
        badges: Badge[];
        accountStatus: AccStatus | null;
    }[];
}


private initializeUser(): IUser{
    return {
      firstName: '',
      lastName: '',
      accounts:[{        // Don't forget the [...], it's an array
        accType: null,
        badges: [],
        accountStatus: null
      }]
    };
  }
Sign up to request clarification or add additional context in comments.

Comments

0
private initializeUser(): IUser{
    return {
      id:0,
      firstName: null as string,
      lastName: null as string,
      accounts:{
         userID: null as number,
        accType: null as accType,
        badges: null as Badge[],
        accountStatus: null as AccStatus
        }
    };
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.