Why did I get this.http.get (...). subscribe is not a function in angular2

My code

  import 'rxjs / Rx';
     ...
     let _this = this;
     return new Promise (function (resolve, reject) {
         _this.http [method] (url, data, {
             headers: headers
         })
         .toPromise ()
         .then (
             (data) => {
                 resolve (data);
             },
             error => {
                 reject (error);
             }
         );
     });

"subscribe" fails from my code, it looks like the angular original.

Error message:

  EXCEPTION: Error: Uncaught (in promise): TypeError: _this.http.get (...). Subscribe is not a function
+5
source share
1 answer

You are fighting the Angular2 initiative of moving from an asynchronous promise based paradigm to an alternative to reactive-extensions . Instead of using a promise, use subscribe instead:

 import 'rxjs/Rx'; ... invoke<T>(onNext: (data: T) => void, onError: (error: any) => any) { this.http[method](url, data, { headers: headers }) .map(response => response.json() as T) .subscribe(onNext, onError); }); 

I also wrote a blog post.

https://ievangelist.imtqy.com/blog/angular-2-http/

+1
source

All Articles