Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use map operator of rxjs in Angular project

I tried to use map operator of rxjs in my Angular project but error below occurs

ERROR in src/app/core/service/session.service.ts(88,9): error TS2552: Cannot find name 'map'. Did you mean 'Map'?

Am I missing anything?

Angular CLI 7.0.4

Node 10.13.0

rxjs 6.3.3

typescript 3.1.6

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';// This is where I import map operator
import { SessionService } from '../service/session.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(private session: SessionService,
    private router: Router) {
  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> |boolean {
    return this.session
  .checkLoginState()//Returns an Observable of Session
  .pipe(
    map(session => {
      if (!session.login) {
        this.router.navigate(['']);
      }
      return session.login;
    })
   )
 }
}
like image 981
M. Yamashita Avatar asked Nov 27 '18 04:11

M. Yamashita


1 Answers

My error happened, because I forget to check the imports of that functions, VS code always do it but sometimes not. Check if it's the same case, just add...

import { map, catchError } from 'rxjs/operators';
like image 119
qleoz12 Avatar answered Oct 21 '22 15:10

qleoz12