如何 return 结合另一个可观察结果的可观察结果?

How to return an observable that combines results of another observable?

目标是从 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=>test.results =results),但是 result 就不会是 Observable<Test>

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

非常感谢您的帮助

使用 map 而不是 tap 并在更改其 results.

后让它成为 test 对象 returns

尝试以下操作:

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