Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 HTTP property retryWhen doesn't exist on type Observable<any>

I am trying to retry my requests so that I can get a refresh token before sending the user to log in again however I am getting an error when I try to use retryWhen on the request and I'm not sure why.

http.service.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Http, Headers, RequestOptions, Response } from '@angular/http';

@Injectable()
export class HttpService {

  constructor(
    private http: Http
  ) { }

  public get(url: string, headers?: Headers): Observable<any> {
    return this.http.get(url, this.getHeaders(headers))
      .map(this.extractData)
      .retryWhen((error: any) => { ... }) // [ts] Property 'retryWhen' does not exist on type 'Observable<any>'.
      .catch(this.handleError);
  }
}
like image 267
efarley Avatar asked Jun 05 '17 19:06

efarley


Video Answer


2 Answers

You must import the operator first before using it(it works the same for other operators)

Put this in your imports at the top of the file:

import 'rxjs/add/operator/retryWhen';

It adds this operator on-top of the Observable prototype.

like image 78
Omri Luzon Avatar answered Sep 21 '22 23:09

Omri Luzon


After using it for a couple days the original solution posted by Omri caused some issues where the property couldn't be found occasionally. (It worked in some builds and not others). I ended up finding a better solution that works more consistently and doesn't require an additional import.

Instead of importing Observable from rxjs/Observable import it from rxjs/Rx.

This DOES NOT work:

import { Observable } from 'rxjs/Observable';

This DOES work:

import { Observable } from 'rxjs/Rx';
like image 41
efarley Avatar answered Sep 17 '22 23:09

efarley