Ошибка при попытке вызвать rest API в angular 7 с помощью HttpClient - PullRequest
0 голосов
/ 29 марта 2020

Я пытаюсь вызвать rest-api из моего angular кода: -

service.ts

@Injectable({ providedIn: 'root' })
export class AssociationService {
  constructor(private httpClient: HttpClient) { }

  getAll() {
    return this.http.get<AssociationResponse>('https://abc/getassociation');
  }
}

component.ts

 ngOnInit() {
        this.associationService.getAll()
          .subscribe((data: any) => this.associations = data,
            error => {},
            () => {});
      }

    constructor(private associationService: AssociationService) {}

AssociationResponse.ts

export class AssociationResponse extends APIResponse {
  constructor(message: string, status: string) {
    super(message, status);
  }
  public associations: Association[];
}

export interface Association {
  id: string;
  name: string;
  adminName: string;
  email: string;
  url: string;
  contactNumber: string;
  password?: string;
}

APIResponse.ts

export class APIResponse {
  message: string;
  status: string;
  constructor(message: string, status: string) {
    this.message = message;
    this.status = status;
  }
}

app.module.ts

providers: [AssociationService, 
    {provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true},
    {provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true},

    // provider used to create fake backend
    fakeBackendProvider,
  ],

Вызов API вызывает следующую ошибку: -

TypeError: Cannot read property 'message' of undefined
    at CatchSubscriber.selector (error.interceptor.ts:19)

сообщение является одним из атрибутов Json response.But если он вызывает API ниже, он работает нормально.

this.httpClient.get<AssociationResponse>
    ('https://abc/getassociation')
      .subscribe(response => {
        this.associations = response.associations;
      });

Может кто-нибудь, пожалуйста, дайте мне знать, что я делаю не так.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...