【问题标题】:retrive data angular firebase检索数据角度火力基地
【发布时间】:2020-01-03 08:48:54
【问题描述】:
this.studentObservable = this.studentDetailsService.readStudentDatabase( user.uid ) as Observable<StudentDetails> ;

this.studentObservable.subscribe( temp => {
         this.student = temp as StudentDetails ;
    });

我已从 firestore 检索数据集合。 而这个this.student 有一个名为eventId 的属性。我需要从 Firestore 中检索 eventId 的文档。如何在没有订阅的情况下做到这一点??

【问题讨论】:

  • 你能在这里写更多代码吗?

标签: angular firebase google-cloud-firestore


【解决方案1】:

有两种方法可以防止回调地狱(嵌套订阅)。

1.通过方法调用你的下一个 http 调用。

this.studentObservable.subscribe(temp => {
  this.student = temp as StudentDetails;
  this.subscribeSomething();
});
subscribeSomething(): void { this.somethingObservable(this.student).subscribe(() => {}); }

2。使用承诺

async method(): Promise<void> {
  try {
    this.student = await this.studentObservable.toPromise();
  } catch(e) {
    throw Error(e);
  }
  try {
    const res = await this.anotherHttpToPromise(this.student);
  } catch(e) {
    throw Error(e);
  }
}

【讨论】:

  • 你能给我一个 wat 把 studentObservable 解压成一个普通的对象,我可以在任何地方使用吗?
  • 我不明白
【解决方案2】:

您可以在 Map 中获取数据,然后您可以在其中使您的逻辑成为您想要做的事情

this.studentObservable = this.studentDetailsService.readStudentDatabase( user.uid ) as Observable<StudentDetails> ;

this.studentObservable
.pipe(map(data))
.subscribe( data => {
         this.student = temp as StudentDetails ;
    });

【讨论】:

  • 在map(data)中,什么是数据?
  • 这样做有什么好处?
  • 这样你就可以在subscribe方法之前获取数据并转换成任何逻辑
猜你喜欢
  • 2016-09-17
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多