Search code examples
javascriptangularfirebase-authenticationangularfireswitchmap

Error implementing .switchMap in Angular project


I'm am learning firebase to implement with my angular projects but running in to a issue with the .switchMap() operator. My understanding is that it is supposed to get one observable and switch to another. I am trying to get firebase user and switch to a authenticated application user.

Error: AngularFireObject<{}> is not assignable to the parm of type (value: User, index: Number)

Here is my code

AuthGuard :

  canActivate(): Observable<boolean>{
    return this.auth.user$
     .switchMap(user => this.userService.get(user.uid)) <-- error
     .map(appUser => appUser.isAdmin)
 }

UserService:

  get(uid:string){
   return this.db.object('/users/' + uid);
}

Thanks


Solution

  • The object() method of the AngularFireDatabase service doesn't return an observable, it returns an AngularFireObject. This service allows you to data operations as well as create observable streams in different ways.

    Instead use one of the observable stream methods provided like valueChanges().

    get(uid: string){
      return this.db.object('/users/' + uid).valueChanges();
    }