【问题标题】:Return inner observable with which operator?使用哪个运算符返回内部可观察对象?
【发布时间】:2019-04-10 22:26:07
【问题描述】:

chat.service.ts:

  getUserChats(): Observable<Chat[]> {
    return this.auth.currUser
      .pipe(
        take(1),
        flatMap(user => this.afs
          .collection('chats', ref => {
            console.log(user.uid)
            return ref.where('members', 'array-contains', `/users/${user.uid}`);
          })
          .snapshotChanges()
          .pipe(
            map(actions => {
              return actions.map(action => {
                const data = action.payload.doc.data() as Chat;
                const id = action.payload.doc.id;
                return {id, ...data};
              });
            })
          ) as Observable<Chat[]>)
      );
  }

有没有办法返回保存用户聊天的内部 Observable?

this.afs
              .collection('chats', ref => {
                console.log(user.uid)
                return ref.where('members', 'array-contains', `/users/${user.uid}`);
              })
              .snapshotChanges()
              .pipe(
                map(actions => {
                  return actions.map(action => {
                    const data = action.payload.doc.data() as Chat;
                    const id = action.payload.doc.id;
                    return {id, ...data};
                  });
                })
              ) as Observable<Chat[]>

在内部 observable 中,我需要等待 user.uid 来查询聊天记录。

【问题讨论】:

  • 什么是内部 Observable?
  • @martin Firestore 查询:this.afs.collection('chats' .....
  • 只需使用concatMap 而不是flatMap
  • 还是不行@martin

标签: typescript firebase rxjs google-cloud-firestore observable


【解决方案1】:

Switchmap 没问题,可以很好地使用它,问题是“数组包含”不适用于 Firestore 中的 DocumentReferences

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 2019-01-30
    • 2017-07-12
    • 2019-02-18
    • 2020-11-05
    • 2018-04-11
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多