如何在Firestore中访问文档

时间:2018-09-27 12:39:17

标签: angular firebase google-cloud-firestore angularfire2

我有一个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;

我已经看过很多教程,但是找不到出现错误的原因

1 个答案:

答案 0 :(得分:3)

需要订阅valueChanges

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