代码之家  ›  专栏  ›  技术社区  ›  Hamed Baatour

注销用户后引发AngularFire2错误

  •  0
  • Hamed Baatour  · 技术社区  · 9 年前

    无法在第行读取null的属性“uid”

    这是 uid ngOnInit

    ngOnInit() {
        this.authSub1 = this.auth.authState.subscribe((auth) => {
            this.tasksSub = this.af.list(`users/${auth.uid}/tasks`, { preserveSnapshot: true }).subscribe(snapshots => {
                snapshots.forEach(snapshot => {
                    this.tasksArray.push(snapshot.val().name);
                });
            });
        }, (err) => {this.openSnackBar(); console.log(err); });
    }
    

    然后我打电话 unsbuscribe 在上述订阅上,以避免在 ngOnDestroy 方法:

    ngOnDestroy() {
        this.authSub1.unsubscribe();
    }
    

    以下是由位于主组件上的按钮触发的注销方法(主组件的路由器出口显示上述组件):

    logout() {
        this.auth.auth.signOut()
        .then(() => { 
            this.router.navigateByUrl('/login'); 
        })
        .catch(function(err) {console.log(err); });
    }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Hamed Baatour    9 年前

    找到了!解决方案非常简单:

    uid authState.subscribe 回调参数,只需使用新创建的变量来存储 uid AngularFireAuth ngOnInit :

    this.userId = this.auth.auth.currentUser.uid;
    
    推荐文章