【问题标题】:Read document from AngularFireStore从 AngularFireStore 读取文档
【发布时间】:2018-08-28 06:37:15
【问题描述】:

我是 firebase 的新手,所以我需要一些非常基本的帮助。

我在 Firestore 中收集了“用户”,当我有带有密钥(用户 ID)的文档并且每个文档都有一些字段时。

在我的组件中,我有变量 userData: Observable<any[]>

用户登录后,我想在文档中找到他的 id 并将他的信息获取到 userData 变量。

  ...
  .then(user => {
    this.authState = user;

    // Here i want get user data and set it to my variable
    this.userData = this.afs.collection('Users').document(user.uid);


    this.router.navigate(['/']);
  })

但显然,这是行不通的。对于我的问题的任何帮助或提示,我将不胜感激,谢谢&

【问题讨论】:

    标签: typescript firebase google-cloud-firestore angularfire2


    【解决方案1】:

    而不是在登录 Promise 后的.then 中做这一切;在您的init 中映射AngularFireAuthauthState observable。

    this.userData = this.afAuth.authState
      .switchMap { user => user ? this.afs.document("Users/${user.uid}").valueChanges() : of(null) }
    

    所以现在我们有一个当前用户数据随时间变化的 Observable 或 null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多