注销时无法读取null的属性“ uid”

时间:2019-11-19 11:08:58

标签: firebase ionic4

每次我从应用程序注销时,我都会得到

  

无法读取null的属性'uid'

从控制台

。它导致引用以下代码:

constructor( private firestore: AngularFirestore, private auth: AuthService, ) 
{
firebase.auth().onAuthStateChanged(user => {
  if (this.currentUser == user) {
    this.currentUser = firebase.auth().currentUser;
    this.vendor = firebase.firestore().doc(`/Products/${this.currentUser.uid}`);
  }
});

1 个答案:

答案 0 :(得分:1)

只要用户的身份验证状态更改(无论他们登录还是注销),onAuthStateChanged回调都会触发。在后一种情况下,user将是null,您现在无法在代码中正确处理它。

这看起来更正确:

firebase.auth().onAuthStateChanged(user => {
  this.currentUser = firebase.auth().currentUser;
  if (this.currentUser) {
    this.vendor = firebase.firestore().doc(`/Products/${this.currentUser.uid}`);
  }
});