Lucien Dubois
Lucien Dubois

Reputation: 1694

Property 'mergeMap' does not exist on type 'Observable<any>'

I'm creating an HttpInterceptor in an Ionic 4 app. I would like to read the Bearer Authorization token from local storage.

I try to use mergeMap but it always return an error:

Property 'mergeMap' does not exist on type 'Observable<any>'

Here is the complete code from token.interceptor.ts

import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError, from  } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Router } from '@angular/router';
import { Storage } from '@ionic/storage';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {

    token: any;

    constructor(private router: Router, private storage: Storage) {}

    intercept(request: HttpRequest < any > , next: HttpHandler): Observable < HttpEvent < any >> {

        return from(this.storage.get('User')).mergeMap((val) => {
            if (this.token) {
                request = request.clone({
                    setHeaders: {
                        'Authorization': this.token
                    }
                });
            }

            if (!request.headers.has('Content-Type')) {
                request = request.clone({
                    setHeaders: {
                        'content-type': 'application/json'
                    }
                });
            }

            request = request.clone({
                headers: request.headers.set('Accept', 'application/json')
            });

            return next.handle(request).pipe(
                map((event: HttpEvent < any > ) => {
                    if (event instanceof HttpResponse) {
                        console.log('event--->>>', event);
                    }
                    return event;
                }),
                catchError((error: HttpErrorResponse) => {
                    if (error.status === 401) {
                        if (error.error.success === false) {
                            // this.presentToast('Login failed');
                        } else {
                            this.router.navigate(['/']);
                        }
                    }
                    return throwError(error);
                }));
        })

    }

}

According to this question, I tried this format:

return from(...).pipe(mergeMap(...));

but it doesn't work.

What should I try?

Upvotes: 1

Views: 2591

Answers (2)

jo_va
jo_va

Reputation: 13963

You are using the syntax of the old RxJS API:

import 'rxjs/operators/mergeMap';
myObs$.mergeMap(anotherObs$);

Since the RxJS API changed (version 5.5+), mergeMap is no longer a method on Observable but a function and you have to use it in the pipe operator, like this:

import { mergeMap } from 'rxjs/operators';
myObs$.pipe(mergeMap(anotherObs$));

Upvotes: 3

Adrian Brand
Adrian Brand

Reputation: 21638

Update your imports

import { map, catchError, mergeMap } from 'rxjs/operators';

Then you can use mergeMap in a pipe.

const { of } = rxjs; // Using destructuring over import because of snippets
const { mergeMap } = rxjs.operators;

of(4).pipe(
  mergeMap(val => of(val * 2))
).subscribe(result => {
  console.log(result);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script>

Upvotes: 0

Related Questions