Property 'toPromise' does not exist on type 'Observable<Response>'

MiHawk picture MiHawk · Jun 29, 2016 · Viewed 53.4k times · Source
import { Headers, Http } from '@angular/http';

@Injectable()
export class PublisherService{

    private publishersUrl = 'app/publisher';

    constructor(private http: Http) { }

    getPublishers(): Promise<Publisher[]>{
        return this.http.get(this.publishersUrl)
                   .toPromise()
                   .then(response => response.json().data) 
                   .catch(this.handleError);
    }
}    

I am getting this error:

Property 'toPromise' does not exist on type 'Observable'.any

Answer

Dinistro picture Dinistro · Jun 29, 2016

You need to add the operator like this:

import 'rxjs/add/operator/toPromise';

This is needed for every rxjs operator you want to use.