【问题标题】:How to return an observable that combines results of another observable?如何返回一个结合了另一个可观察结果的可观察对象?
【发布时间】:2021-10-31 22:43:07
【问题描述】:

我们的目标是从 firestore 中检索一个文档,并且对于每个文档,使用来自另一个集合的文档数组来完成它。

Firestore 不支持子集合的“包含”。

在我的例子中,我有一个Test,它有一个完整的“TestResults”数组。测试结果的名称是firebase ID。

到目前为止,我有以下内容:

const result = this.firestore
         .doc<Test>(`tests/${testId}`)
         .valueChanges({ idField: 'id' })
         .pipe(
            switchMap((test) =>
               this.firestore
                  .collection<TestResult>(`tests/${test.id}/results`)
                  .valueChanges({ idField: 'name' })
                  .pipe
                  //How to assign my testResults[] to my current test
                  //And how to return a test?
                  ()
            )
         );

我想我会写一个tap(results=&gt;test.results =results),但是result 不会是Observable&lt;Test&gt;

您将如何实现这一目标?将使用相同的逻辑来获取所有测试。

非常感谢您的帮助

【问题讨论】:

  • 使用map而不是tap,让map在改变results后返回test对象

标签: angular rxjs observable rxjs-observables


【解决方案1】:

使用map 代替tap 并让它在更改其results 后返回test 对象。

尝试以下方法:

const result = this.firestore
  .doc<Test>(`tests/${testId}`)
  .valueChanges({ idField: 'id' })
  .pipe(
    switchMap(test =>
      this.firestore
        .collection<TestResult>(`tests/${test.id}/results`)
        .valueChanges({ idField: 'name' })
        .pipe(
          map(results => {
            test.results = results;
            return test;
          })
        )
    )
  );

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 2020-06-03
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多