ERROR TypeError: platformFormService.getTaskCandidateGroupId is not a function

Есть класс с такой функцией.

@Injectable()
export class PlatformFormService {
 public getTaskCandidateGroupId(): string {
      const task: any = this.platformStateService.getSelectedTask();
      if (task && task._embedded && task._embedded.identityLink && task._embedded.identityLink.length > 0) {
        return task._embedded.identityLink[0].groupId;
      }
      return undefined;
  }
}

в разных классах функция работает корректно. Но вот тут ее вызовы валиться с ошибкой.

export function validateDocuments(platformService: PlatformFormService): ValidationErrors | null {

  const platformFormService: any =  PlatformFormService;
  const candidateGroupId: string = platformFormService.getTaskCandidateGroupId();
}

как мне функцию getTaskCandidateGroupId() ее вызвать?

если вызываю через переменную в методе вот так

const candidateGroupId: string = platformService.getTaskCandidateGroupId();

то падает ошибка ERROR TypeError: Cannot read property 'getTaskCandidateGroupId' of undefined


Ответы (1 шт):

Автор решения: Grundy

В данном методе лишняя строчка

const platformFormService: any =  PlatformFormService;

В ней в локальную переменную сохраняется сам класс. Так как метод не статичный, при попытке вызвать его непосредственно у класса - приводит к ошибке.

Вместо этого нужно воспользоваться переданным параметром. Для этого достаточно удалить указанную строку.

→ Ссылка