【问题标题】:how to terminate pipe operation in @ngrx/effects?如何在@ngrx/effects 中终止管道操作?
【发布时间】:2019-02-03 21:43:05
【问题描述】:

在下面的代码中,我从 Firestore 中获取了可观察到的集合长度。

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

并且这个方法被@ngrx/effects 访问如下

@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);
                    })
                )
    }),
)

在组件中,我使用 store 方法在检查用户是否存在后调度操作

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

只要用户登录,一切正常。当用户注销时,它会引发如下错误

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

我对这个错误的理解是管道操作仍在尝试访问数据库

有什么方法可以关闭ngrx/effects中的管道操作或任何其他方法请帮助我!

【问题讨论】:

标签: angular google-cloud-firestore ngrx-store ngrx-effects


【解决方案1】:

您什么时候从stream1 到另一个stream2 执行switchMap - 仔细检查您是否真的想听除第一个之外的每个stream2 操作。

看来解决的办法是在效果的内层流中加入first()

【讨论】:

  • 使用 first() 给我错误:@firebase/firestore: Firestore (5.0.4): Could not reach Cloud Firestore backend 后端在 10 秒内没有响应。我认为 first() 发出一个值然后关闭连接,在这种情况下,我一直需要后端连接
猜你喜欢
  • 2019-01-11
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多