Saturday, 9 February 2019

Returning an array of strings from httpClient using Observables

I have an API endpoint that's returning an array of strings (json) and I'm trying to create a page that spits out the contents via an Angular Service. Here's what I have thus far (I'm on Angular 7):

export class FieldService {
  constructor(private httpClient: HttpClient) { }

  fieldTypesURL = 'http://localhost:3000/fields/types';

  public getTypes(): Observable<any[]> {
    return this.httpClient.get<any[]>(this.fieldTypesURL)
    .pipe(map((response: any) => response.json()),
      catchError((error: any) => Observable.throw(error.json().error || 'Server error')));
  }
}

The compilation error I'm getting is the following:

Type 'Observable<any[]>' is missing the following properties from type 'Promise<string[]>': then, catch, [Symbol.toStringTag], finally

Why is it mentioning a Promise here while I'm trying to use an Observable? Any ideas welcome!



from Returning an array of strings from httpClient using Observables

No comments:

Post a Comment