2017-08-02 120 views
0

我使用Angularfire2库在用户登录并获取从火力地堡数据库中的数据,但是当用户点击退出以下错误抛出到控制台后抛出:AngularFire2错误注销用户

不能读取线零的特性“UID”

这里是uid财产在ngOnInit方法任务组件的2线引发错误:

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

回答

0

发现它!该解决方案是非常简单的:

避免用户ID为空当用户点击注销我方才避免使用uidauthState.subscribe回调参数,只是用一个新创建的变量,而不是存储直接uid从注入的AngularFireAuth服务ngOnInit

this.userId = this.auth.auth.currentUser.uid;