0

So I had a function in Angular named appUser$, the error said "Cannot destructure property 'uid' of 'object null' as it is null."

  get appUser$(): Observable<AppUser> {
    return this.user$.pipe(
      switchMap(({uid})=> this.userService.get(uid)));
  }

Then I changed it to:

  get appUser$(): Observable<AppUser> {
    return this.user$.pipe(
      switchMap(({uid})=> {
        if({uid}){
          return this.userService.get(uid);
        } else {
          return of(null);
        }
      }));   
  }

But still it doesnt work, any ideas?

2 Answers2

1

Is there any specific reason why the argument is enclosed as an object? The de-structuring issue is stemming from this assignment. You could try to use it directly

get appUser$(): Observable<AppUser> {
  return this.user$.pipe(
    switchMap(uid => this.userService.get(uid))
  );
}
ruth
  • 29,535
  • 4
  • 30
  • 57
0

I am not sure, but I think you have an issue with the function argument: I mean the argument is null and you are trying to get the uid from null.

try like this:

 get appUser$(): Observable<AppUser> {
    return this.user$.pipe(
      switchMap((obj)=> {
        if(obj && obj.uid){
          return this.userService.get(obj.uid);
        } else {
          return of(null);
        }
      }));   
 }
Ashot Aleqsanyan
  • 4,252
  • 1
  • 15
  • 27