0

I am using "rxjs": "~6.2.0" in below code block

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

The get method of userService has below logic.

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

I am getting error

"Argument of type '(user: User) => AngularFireObject' is not assignable to parameter of type '(value: User, index: number) => ObservableInput'. Type 'AngularFireObject' is not assignable to type 'ObservableInput'. Type 'AngularFireObject' is not assignable to type 'Iterable'. Property '[Symbol.iterator]' is missing in type 'AngularFireObject'."

2 Answers 2

4

You need not use two pipes here, map the result appUser.isAdmin using the same pipe.

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

Also you should modify userService.get() to return proper Observable which you get from valueChanges()

get(uid : string): Observable<any>
{
     return this.db.object('/users/'+ uid).valueChanges();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Amit, thank you for help.but I'm getting same error in switchmap . Can you please help me in this? Here the logic I want to achieve is switchmap should map user to appUser which we get from userService.then map additional property isAdmin to appUser.
Can you add more code? Probably UserService class as well.
Here is userService get method code. get(uid : string): AngularFireObject<AppUser> { return this.db.object('/users/'+ uid); }
Why don't you return return this.db.object('/users/'+ uid).valueChanges(); from get() method and declare its return type as Observable<AppUser>
Thank you Amit, but I am getting error in this "Type 'Observable<{}>' is not assignable to type 'Observable<AppUser>'. Type '{}' is not assignable to type 'AppUser'. Property 'name' is missing in type '{}'."
|
0

The argument passed to SwitchMap should be an Observable, but this.userService.get(user.uid) returns a AngularFireObject<AppUser>.

AngularFireObject is from AngularFire. I haven't used AngularFire, but from what I get you have to call valueChanges() on the AngularFireObject in order to get an Observer. So if you can change the UserService, it should look like this:

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

And as Amit Chigadani mentioned, you should put the switchMap and map in the same pipe, as well as simplifying the arrow function in the switchMap.

1 Comment

Thank you for help Amit provided answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.