Google cloud firestore 无法读取';uid';空的

Google cloud firestore 无法读取';uid';空的,google-cloud-firestore,firebase-authentication,ionic4,Google Cloud Firestore,Firebase Authentication,Ionic4,我需要帮助,我使用firebase作为后端,Ionic 4作为前端 我在控制台上有错误,当我第一次打开/使用应用程序时,我在控制台上收到无法读取null的'uid'属性 我读了一些相关的解决方案,但我还没有找到一个解决我的问题 下面是我在应用程序中实现它时的代码: initializeApp() { this.afAuth.auth.onAuthStateChanged(user => { const userId = user.uid; this.db.doc(`account

我需要帮助,我使用firebase作为后端,Ionic 4作为前端

我在控制台上有错误,当我第一次打开/使用应用程序时,我在控制台上收到无法读取null的'uid'属性

我读了一些相关的解决方案,但我还没有找到一个解决我的问题

下面是我在应用程序中实现它时的代码:

initializeApp() {

this.afAuth.auth.onAuthStateChanged(user => {
  const userId = user.uid;
  this.db.doc(`accounts/${userId}`).valueChanges().pipe(
    take(1)
  ).subscribe(userData => {
    const role = userData['role'];
    if (role == 'USER') {
      console.log('we got a customer user');
      this.router.navigateByUrl('/tabs/home')
    } else if (role == 'VENDOR') {
      console.log('we got a seller user');
      this.router.navigateByUrl('/vendor-tabs/home-vendor')
    }
  })
})
}

当用户登录和注销时,将调用身份验证状态更改侦听器<如果未登录,代码>用户将为空。这意味着您必须先检查
user
是否真实,然后才能开始访问其属性

this.afAuth.auth.onAuthStateChanged(user => {
  if (user) {
    // user is signed in
    const userId = user.uid;
  }
  else {
    // user is signed out - can't use properties of user.
  }
}

请参阅。

这是我为摆脱'uid'null的不可读取属性所做的

this.afAuth.auth.onAuthStateChanged(user => {
  if (user) {
    const userId = user.uid;
    this.db.doc(`accounts/${userId}`).valueChanges().pipe(
      take(1)
    ).subscribe(userData => {
      const role = userData['role'];
      if (role == 'USER') {
        console.log('we got a customer user');
        this.router.navigateByUrl('/tabs/home')
      } else if (role == 'VENDOR') {
        console.log('we got a seller user');
        this.router.navigateByUrl('/vendor-tabs/home-vendor')
      }
    })
  } else {
    return of(null);
  }
})

如果你的解决方案不同,你能详细回答这个问题吗?否则,请将我的答案标记为正确,以便将来其他人可能会从中受益。