Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 637
Abhishek Sharma Avatar asked Jun 29 '16 05:06

Abhishek Sharma


3 Answers

You need to add the operator like this:

import 'rxjs/add/operator/toPromise';

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

like image 200
Dinistro Avatar answered Oct 12 '22 15:10

Dinistro


Try adding 'Response' to your import statement from '@angular/http' like this :

import {Http, Headers, Response} from '@angular/http';

Also i noticed you don't import Ingectable from angular core although you use @Injectable decorator.

import { Injectable } from '@angular/core';
like image 30
Shai Barak Avatar answered Oct 12 '22 15:10

Shai Barak


use this import at the beginning

import {Observable} from "rxjs/Rx";
like image 5
imal hasaranga perera Avatar answered Oct 12 '22 15:10

imal hasaranga perera