Http in the latest version of Angular and Ionic2 Alpha 46

Does anyone know what has changed in the latest version of Angular with regards to http and/or the observable it returns? I’ve just upgraded my Ionic 2 app from alpha40 to alpha46 and the code below (which worked ok in 40) no longer works in 46. I get an error of "undefined is not a function near ‘.timeout(5000)’. I’m not sure what function the error is referring to. I tried removing the timeout line but that didn’t fix it. I also removed the call to loginResult() and I still got the error. And I also removed the .map and that didn’t fix it either. I’m a bit stumped!

login(credentials) {
   var username = credentials.username;
   var password = credentials.password;

   var parms = "Username=" + username + "&Password=" + password;
   this.http.post(MY_SERVER, parms, {
   headers:  { 'cache-control':'no-cache','Accept':'application/json','content-type':    'application/x-www-form-urlencoded' }
   })
   .timeout(5000)
   .map((res: Response) => res.text())
   .subscribe(
     res => this.loginResult(res),
     err => this.loginError(err),
     () => console.log('Login Complete')
    );
  }

   loginResult(res) {
     console.log('Login Result');
  }

  loginError(err) {
     console.log('Login Error');
  }

First show your dependencies, then your constructor and maybe then we will be able to help (preferably both in the same post).

Most likely you need some extra dependencies, now ionic 2 doesn’t include by default the .timeout() nor .map() operator from Rxjs and now you have to include it yourself if you need them, for example:

import ‘rxjs/add/operator/retry’;
import ‘rxjs/add/operator/timeout’;
import ‘rxjs/add/operator/delay’;
import ‘rxjs/add/operator/map’;

1 Like

Thank you - I will take a look at that and if I still have issues I will re-post with the constructor etc.

Thanks, that did fix it. I didn’t realise the Angular team had made those changes. There’s a thread about it here if anyone’s interested.

Glad you hadn’t to crash with a wall for a few weeks before realizing this (its not like i did crash :smirk:)

1 Like