Angular - Почему я получаю объект объектов вместо массива объектов? - PullRequest
0 голосов
/ 27 апреля 2020

Компонент:

applicants: UserProfile[];
ngOnInit() {
    this.store
      .pipe(select(fromRootUserProfileState.getUserProfiles))
      .subscribe((userProfiles: UserProfile[]) => {
        this.applicants = userProfiles;
        console.log('this.applicants:', this.applicants);
      });
}

Интерфейс UserProfile:

export interface UserProfile {
  id: number;
  fullName: string;
  roles?: Role[];
  windowsAccount?: string;
  firstName?: string;
  lastName?: string;
  email?: string;
  managerName?: string;
  managerId?: number;
  managerEmail?: string;
  companyId?: number;
  companyName?: string;
  countryId?: number;
  country: Country;
  countryName?: string;
}

Эффект NgRx:

@Injectable()
export class UserProfileEffects {
  constructor(
    private actions$: Actions,
    private userProfileService: UserProfileService
  ) {}
  @Effect()
  loadUserProfiles$: Observable<Action> = this.actions$.pipe(
    ofType(userProfileActions.UserProfileActionTypes.LoadUPs),
    mergeMap((action: userProfileActions.LoadUPs) =>
      this.userProfileService.getUserProfiles().pipe(
        map(
          (userProfiles: Array<UserProfile>) =>
            new userProfileActions.LoadSuccessUPs(userProfiles)
        ),
        catchError((err) => of(new userProfileActions.LoadFailUPs(err)))
      )
    )
  );
}

Служба:

getUserProfiles(): Observable<UserProfile[]> {
    return this.http.get<UserProfile[]>(this.baseUrl + 'userprofiles/getuserprofiles');
  }

Backend API (. Net Core):

[HttpGet("[action]")]
public async Task<IActionResult> GetUserProfiles()
{
var userProfiles = await _userProfileRepository.GetUserProfiles();
var userProfileViewModels = _mapper.Map<IEnumerable<UserProfileViewModel>>(userProfiles);

return Ok(userProfileViewModels);
}

Но в компоненте я не могу рассматривать свой массив «заявителей» как правильный массив, это своего рода объект. Это происходит по всему моему коду, почему я не получаю массив объектов, как я определил явно?

enter image description here

enter image description here

1 Ответ

0 голосов
/ 27 апреля 2020

Я могу предложить обходной путь на данный момент. Вы можете преобразовать ответ в массив из приведенного ниже кода.

for (var key in userProfiles ) {
    if (userProfiles.hasOwnProperty(key)) {
        var val = userProfiles[key];
        this.applicants.push(val)
    }
}

После этого вы можете выполнить переменную pu sh операция applicants.

...