how to terminate pipe operation in @ngrx/effects?

Viewed 612

In below code, I'm getting the length of the collection as observable from Firestore.

getUserCartLength(uid:string){
    return this.db.collection('users/'+uid+'/cart').valueChanges()
        .pipe(
            map(docs=>{return docs.length})
        )
}

And this method is accessed by the @ngrx/effects as follows

@Effect()
userExist$ = this.actions$.ofType(UserAuthTypes.YES_USER_EXIST).pipe(
    map((action:YesUserExist)=>action.user),
    switchMap((user)=>{
        return this.userService.getUserCartLength(user.uid)
                .pipe(
                    map(length=>{
                        console.log(length);
                        return new InitializeUserCart(length);
                    })
                )
    }),
)

And in the component, I'm using store method to dispatch actions after checking user exists or not

ngOnInit() {
  this.logService.isUserExist().subscribe(user=>{
      if(user) this.store.dispatch(new fromUser.YesUserExist(user));
      else this.store.dispatch(new fromUser.NoUser());
  })
}

everything works fine as long as user logged-In. When user logged-out it throws an error as below

core.js:1673 ERROR Error: Missing or insufficient permissions.

My understanding about this error is that pipe operation is still trying to access database

Is there any way to close pipe operation in ngrx/effects or any other approach please help me !

1 Answers

When do you do switchMap from stream1 to another stream2 - double check that you really want to listen to every stream2 action except first one.

It seems that the solution is to add first() to the inner stream in the effect.

Related