2

Getting this error when I upgraded to Angular 6. I see the documentation to use .pipe(), but I am not getting how to use pipe when there is multiple .map() as below. Need your help...

   import {Injectable} from '@angular/core';
    import {Http, Headers} from '@angular/http';
    import 'rxjs/add/operator/map';
    import {TOKEN_AUTH_PASSWORD, TOKEN_AUTH_USERNAME} from '../services/auth.constant';

    @Injectable()

    export class AuthenticationService {

      static AUTH_TOKEN = '/oauth/token';
      constructor(private http: Http) {
      }
      login(username: string, password: string) {

        const body = `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&grant_type=password`;

        const headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        headers.append('Authorization', 'Basic ' + btoa(TOKEN_AUTH_USERNAME + ':' + TOKEN_AUTH_PASSWORD));
        return this.http.post(AuthenticationService.AUTH_TOKEN, body, {headers})
          //.map(res => res.json())
          .pipe(map((res: any) => {
            if (res.access_token) {
              return res.access_token;
            }
            return null;
          }));
      }
    }

I understand to use .pipe when there is one .map is used like below, but I am not getting how to use pipe() when multiple .map are used.

.pipe(map(data => {})).subscribe(result => {
      console.log(result);
    });
SK.
  • 1,390
  • 2
  • 28
  • 59
  • @user184994 - Sir/Madam, yes, there are so many questions like this. But I didn't find an example/question where more then one .map() are used – SK. Nov 07 '18 at 19:34
  • You just need to comma separate the map functions: `pipe(map(), map())` etc – user184994 Nov 07 '18 at 19:36

1 Answers1

0

to use pipe with multiple operators you should use comma:

return this.http.post(AuthenticationService.AUTH_TOKEN, body, {headers})
.pipe(
  map(res => res.json()),
  map((res: any) => {
    if (res.access_token) {
      return res.access_token;
    }
    return null;
  })
);

but it's not needed to use map(res => res.json()) in your case.

and

In previous versions the import was import 'rxjs/add/operators/map'.

In Angular 6 it should be: import { map } from 'rxjs/operators';

By the way if you're expected the cancelling effect you should use switchMap instead of map. Read more

Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
  • Thanks Dmitry. I will try this code. But I am not sure why some genius guy downvoted without leaving a comment why he/she did so... – SK. Nov 07 '18 at 19:46
  • I've updated an answer. – Dmitry Grinko Nov 07 '18 at 19:54
  • I am accepting the answer as I used your code and not getting that error. But I have some other errors which are because of upgrading Angular to 6. It seems like Angular is so fragile now, in every 2-3 months there are so many changes and the old are not backward compatible... so annoying... – SK. Nov 07 '18 at 19:57
  • Now I am getting the below error: ERROR in ./node_modules/angular2-jwt/angular2-jwt.js Module not found: Error: Can't resolve 'rxjs/Observable' in 'C:\AFS\AFS-WEB\node_modules\angular2-jwt' ERROR in node_modules/angular2-jwt/angular2-jwt.d.ts(3,10): error TS2305: Module '"C:/AFS/AFS-WEB/node_modules/rxjs/Observable"' has no exported member 'Observable'. – SK. Nov 07 '18 at 19:59
  • 1) npm i angular2-jwt 2) import { Observable } from 'rxjs'; – Dmitry Grinko Nov 07 '18 at 20:01
  • Create new question if it is not help – Dmitry Grinko Nov 07 '18 at 20:02
  • Sure, creating a new question after I do some research. – SK. Nov 07 '18 at 20:04
  • installed rxjs-compat. now no error. Lets see by running the application. – SK. Nov 07 '18 at 20:09