2017-08-02 122 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;