【问题标题】:How to access document in firestore如何访问firestore中的文档
【发布时间】:2018-09-27 12:39:16
【问题描述】:

我有一个 firebase 数据库,顶部有一个 users 节点。例如,我想获取该节点的第一个子节点。我正在尝试以这种方式访问​​,但我说:

TypeError: "this.users[0] is undefined"

export class FirebaseService {
  user: Observable<User | null>
  userCollection: AngularFirestoreCollection<User>;
  users: Observable<User[]>;

  getUserByEmail(email) {
     this.userCollection = this.afs.collection('users');
     this.users = this.userCollection.valueChanges();
     let a = this.users[0].name;

我已经看了很多教程,但我找不到错误的原因

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    需要订阅valueChanges

    this.userCollection.valueChanges().subscribe((item) => {
       this.users = item;
       console.log(this.users[0].name)
    })
    

    【讨论】:

    • Type 'User[]' is not assignable to type 'Observable&lt;User[]&gt;'. Property '_isScalar' is missing in type 'User[]'.
    • 更改用户类型users: User[];
    • 如果有帮助,请务必检查答案。
    • 是的,我只是在等待时间限制
    猜你喜欢
    • 2021-06-08
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2019-08-16
    相关资源
    最近更新 更多