从Firestore返回数据给出错误

时间:2019-08-20 20:58:06

标签: angular firebase ionic-framework google-cloud-firestore

好的,我在尝试从Firestore中的集合返回数据时遇到错误。

  

错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ get”

错误指向此处的.get()

ngOnInit() {
  this.noteService
    .getNoteList()
    .get()
    .then(noteListSnapshot => {
      this.noteList = [];
      noteListSnapshot.forEach(snap => {
        this.noteList.push({
          id: snap.id,
          noteBottle: snap.data().noteBottle,
          noteDistillery: snap.data().noteDistillery,
          noteDate: snap.data().noteDate,
        });
        return false;
      });
    });
}

我认为问题确实在我的服务范围内,尽管看起来像这样...

public noteListRef: firebase.firestore.CollectionReference;
public currentUser: firebase.User;

constructor() {
  firebase.auth().onAuthStateChanged(user => {
    if (user) {
      this.currentUser = firebase.auth().currentUser; 
      this.noteListRef = firebase.firestore().collection(`/userProfile/${this.currentUser.uid}/eventList`);
    }
  });
}

任何关于为什么获取的想法都未定义?

1 个答案:

答案 0 :(得分:0)

这是因为在尝试将其与其他内容一起使用之前,您需要解决用户承诺。

此仓库显示了如何在没有这些错误的情况下进行操作:

https://github.com/javebratt/ionic-angular-firebase-authentication-starter/blob/master/src/app/services/auth.service.ts

https://github.com/javebratt/ionic-angular-firebase-authentication-starter/blob/master/src/app/services/profile.service.ts

您使用以下方式让用户使用:

  getUser(): Promise<firebase.User> {
    return this.afAuth.authState.pipe(first()).toPromise();
  }

这意味着您可以像这样使用它:

  async getUserProfile(): Promise<Observable<UserProfile>> {
    const user: firebase.User = await this.authService.getUser();
    this.currentUser = user;
    this.userProfile = this.firestore.doc(`userProfile/${user.uid}`);
    return this.userProfile.valueChanges();
  }