Search code examples
typescriptrxjsobservableangular10

Cannot read properties of undefined (reading 'subscribe')


I've been trying to subscribe to a particular observable(i am a beginner to rxjs), but I keep failing. here is my code


here I need to assign the first element of usergroups array element to this.currentGroup

const selectedUserGroups$ =
this.userGroupsService.getUserGroupsWithUsers(this.selectedUserGroupIds).pipe(shareReplay());

this.selectedUserGroups$.subscribe( (usergroups: UserGroup[]) =>
this.currentGroup = usergroups[0]); 

if(this.currentGroup.users?.length >0) 
{
  this.showUsersList(); 
}

but this won't work as i expected, Appreciate your help regarding this


Solution

  • const selectedUserGroups$ = // assignment here
    this.selectedUserGroups$.subscribe( //...
    

    Those are two different objects: the variable in the function scope selectedUserGroups$ and the object property this.selectedUserGroups$.

    Remove this. from the second line, for example. Or replace const selectedUserGroups$ = with this.selectedUserGroups$ =.