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

5,190 views
Skip to first unread message

Abhishek Sharma

unread,
Jul 4, 2016, 9:29:49 AM7/4/16
to AngularJS
// publisher.service.ts

import { Injectable } from '@angular/core';
import { Publisher } from './publisher';

import { Headers, Http, HTTP_PROVIDERS, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';

@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);
    }
    
    private handleError(error: any) {
      console.error('An error occurred', error);
      return Promise.reject(error.message || error);
    }
    
}

Added following to main.ts and app.component.ts too to try and resolve error:-

import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';


package.json:-

{
  "name": "angular2-seed",
  "version": "1.0.0",
  "scripts": {
    "lite": "lite-server",
    "postinstall": "typings install",
    "gulp": "gulp",
    "start": "concurrently \"npm run gulp\" \"npm run lite\" ",
    "typings": "typings"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.4",
    "angular2-in-memory-web-api": "0.0.14",
    "systemjs": "0.19.27",
    "bootstrap": "^3.3.6",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": " 5.0.0-beta.6",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "gulp": "^3.9.0",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-clean-css": "^2.0.6",
    "gulp-sass": "^2.3.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-typescript": "^2.10.0",
    "lite-server": "^2.2.0",
    "systemjs-builder": "^0.15.16",
    "typescript": "^1.8.10",
    "typings":"^1.0.4"
  }
}


systemjs.config.js:-

(function(global) {

    // map tells the System loader where to look for things
    var map = {
        'app':                        'app', // 'dist',
        'rxjs':                       'node_modules/rxjs',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        '@angular':                   'node_modules/@angular',
        '@angular/router':            'node_modules/@angular/router'
    };

    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        'app':                        { main: 'boot.js',  defaultExtension: 'js' },
        'rxjs':                       { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }
    };

    var packageNames = [
        '@angular/common',
        '@angular/compiler',
        '@angular/core',
        '@angular/http',
        '@angular/platform-browser',
        '@angular/platform-browser-dynamic',
        '@angular/router',
        '@angular/router-deprecated',
        '@angular/testing',
        '@angular/upgrade'
    ];

    // add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
    packageNames.forEach(function(pkgName) {
        packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
    });
    

    var config = {
        map: map,
        packages: packages
    };

    // filterSystemConfig - index.html's chance to modify config before we register it.
    if (global.filterSystemConfig) { global.filterSystemConfig(config); }

    System.config(config);

})(this);
 

Alejandro C

unread,
Jul 25, 2016, 9:20:13 AM7/25/16
to AngularJS
I have the same problem... any suggestion?

Lucas Lacroix

unread,
Jul 25, 2016, 9:22:53 AM7/25/16
to ang...@googlegroups.com
If you are not importing 'rxjs/all', then the Rxjs package is modularized and you need to "add in" each function you want. I suggest you do NOT import the 'all' module as this will bloat your application significantly.

Example:
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise'; // this adds the non-static 'toPromise' operator
import 'rxjs/add/operator/map';         // this adds the non-static 'map' operator

-Luke




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Lucas Lacroix
Computer Scientist
System Technology Division, MEDITECH

Lucas Lacroix

unread,
Jul 25, 2016, 9:27:38 AM7/25/16
to ang...@googlegroups.com
PS - you should NOT import 'rxjs/Rx' as your application will be bloated.

On Mon, Jul 25, 2016 at 9:24 AM, Lucas Lacroix <lcha...@meditech.com> wrote:
Sorry... Missed the part where you're already doing that.

Try trapping out the Observable class (this works in Chrome, and maybe Firefox):
console.log('%O', Observable);

-Luke

Lucas Lacroix

unread,
Jul 25, 2016, 9:27:38 AM7/25/16
to ang...@googlegroups.com
Sorry... Missed the part where you're already doing that.

Try trapping out the Observable class (this works in Chrome, and maybe Firefox):
console.log('%O', Observable);

-Luke

On Mon, Jul 25, 2016 at 9:22 AM, Lucas Lacroix <lcha...@meditech.com> wrote:

Madhav Patel

unread,
Aug 23, 2016, 8:10:42 PM8/23/16
to AngularJS
I am also having the same problem. has anyone found the solution ? Please share.

Thanks.

Lucas Lacroix

unread,
Aug 23, 2016, 8:15:40 PM8/23/16
to AngularJS
I don't think this is a common issue. I've been using Observables and have never run in to this.

Try reproducing in plunkr.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
Lucas Lacroix
Computer Scientist
Advanced Technology Division, MEDITECH
Reply all
Reply to author
Forward
0 new messages