如何将文档添加到Firestore并返回Observable

问题描述 投票:0回答:1

使用@ angular / fire将文档添加到Firestore集合将返回Promise。我想做的是添加文档,但是返回创建的文档的Observable。这是到目前为止我尝试过的内容:

colRef: AngularFirestoreCollection = this.afs.collection<Session>('sessions');   

add(session: Session): Observable<Session> {
         return this.colRef.add(session).then((docRef) {
           return this.colRef.doc<Session>(docRef.id).valueChanges();
         })    
      }

当然,这是行不通的,它正在尝试返回Promise<Observable<Session>>。我知道为什么不起作用,但不知道如何实现我的目标。任何帮助,不胜感激!

angular firebase observable angularfire
1个回答
0
投票

您可以通过from运算符使用RxJS from方法。尝试以下操作

switchMap

switchMap可用于将承诺转换为可观察的。

© www.soinside.com 2019 - 2024. All rights reserved.