1

I'm somewhat new to Angular. I just want to load some products from an API and then create instances of my class (not just objects 'shaped' to my class). So I have a constructor on my Product class that will copy the incoming values.

Here's my method in the service class, which works fine.

getProductList(): Observable<Product[]> {
    return this.http
        .get<Product[]>(this.productListUrl)
        .pipe(
            map(data => data.map(item => new Product(item))),
            catchError(this.handleError));
}

However, I also want to pass a token in the GET request header for authentication. So I'm adding an httpOptions object to the get method call (with an Authorization key/value set in the httpOptions):

@Injectable()
export class ProductService {

// http options used for making API calls
private httpOptions: any;

private apiURL = environment.apiURL;
private productListUrl = this.apiURL + '/api/v1/products/';

constructor(private http: HttpClient) {
    this.httpOptions = {
        headers: new HttpHeaders({'Content-Type': 'application/json'}),
        observe: 'events'
    };
}

getProductList(): Observable<Product[]> {
    return this.http
        .get<Product[]>(this.productListUrl, this.httpOptions)
        .pipe(
            map(data => data.map(item => new Product(item))),
            catchError(this.handleError));
}

private handleError(res: HttpErrorResponse | any) {
    console.error(res.error || res.body.error);
    return observableThrowError(res.error || 'Server error');
}
}

But somehow the addition of the httpOptions argument to the get method causes the return type to change, and the compile now complains Property 'map' does not exist on type 'HttpEvent<Product[]>'. and won't compile.

Any thoughts on whether I'm doing this wrong, or how I would change the way I'm using map? Thanks!

DanielMcQ
  • 37
  • 1
  • 8
  • 2
    What are the options?? Seems you are changing observe property from body to event. – Antoniossss Dec 19 '18 at 23:25
  • Add your httpOptions setter – Rahul Dec 20 '18 at 01:30
  • Check if your `httpOptions` contain `observe: "events"` instead of `observe: "body"`. I think what you want is `observe: "body"`. – frido Dec 20 '18 at 15:46
  • Thanks for comments! I've added my httpOptions declaration to my original question above . I tried adding `observe:'events'` to my httpOptions but still getting compile error `ERROR in src/app/service/product.service.ts(30,34): error TS2339: Property 'map' does not exist on type 'HttpEvent'. Property 'map' does not exist on type 'HttpProgressEvent'.` – DanielMcQ Dec 21 '18 at 00:34
  • @DanielMcQ I'm sorry, I meant `observe: "events"` is probably the problem. You should use `observe: "body"` – frido Dec 22 '18 at 13:23
  • Thanks @fridoo. I tried changing above code to have `observe: 'body'` but still same error :-( – DanielMcQ Jan 02 '19 at 00:48
  • 1
    Possible duplicate of [Angular HttpClient return expecting observable rather than observable](https://stackoverflow.com/questions/48401250/angular-httpclient-return-expecting-observablehttpeventany-rather-than-observ) – frido Jan 02 '19 at 09:34
  • 1
    Also check out https://stackoverflow.com/q/48935277/9423231 – frido Jan 02 '19 at 13:04

0 Answers0