【问题标题】:Returning Data from Firestore giving error从 Firestore 返回数据给出错误
【发布时间】:2019-08-20 20:58:06
【问题描述】:

好的,我在尝试从 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`);
    }
  });
}

关于为什么 get 未定义的任何想法?

【问题讨论】:

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


    【解决方案1】:

    这是因为您需要先解决用户承诺,然后再尝试将其与其他东西一起使用。

    这个 repo 展示了如何在没有这些错误的情况下做到这一点:

    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();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2018-06-07
      • 2020-03-21
      • 1970-01-01
      • 2017-10-21
      • 2017-10-20
      • 2020-02-20
      相关资源
      最近更新 更多