首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子查询AngularFirestoreCollection snapshotChanges结果中的数据

子查询AngularFirestoreCollection snapshotChanges结果中的数据
EN

Stack Overflow用户
提问于 2018-02-01 08:25:08
回答 1查看 415关注 0票数 1

我正在尝试查询一个集合,并根据另一个查询返回更改数据结果。

我的聊天结构是这样的:

代码语言:javascript
复制
let chat = {
  id: "userId1" + "userId2",
  lastMessage: "",
  timestamp: "",
  title: ""
}

我需要拆分聊天id,查询其中一个用户,并将用户名设置为聊天标题。

我试着这样做,但没有工作,也没有在模板上显示结果。

代码语言:javascript
复制
listChats(userId: string): Observable<any[]> {
return <Observable<any[]>>this.afs.collection(this.CHATS_COLLECTION)
  .snapshotChanges()
  .map((c) => {
    return c
      .filter((chat) => chat.payload.doc.id.startsWith(userId) || chat.payload.doc.id.endsWith(userId))
      .map(c => {
        const chatData = c.payload.doc.data() as Chat;
        const chatId = c.payload.doc.id;

        //the id here comes like "id1 + id2"
        let ids: string[] = c.payload.doc.id.split(this.SEPARADOR);

        //get the id 
        let id =  ids[0];

        //check in firebase the user row
        return this.afs.doc("users/" + id)
            .snapshotChanges()
            .map((c) =>{
                const data = c.payload.data() as User;
                let name = data.name;

                //Here I need to get the user name and set on the chat data
                chatData.title = name;

                return { chatId, ...chatData };

            });

      });

  });}

ChatPage.ts:

代码语言:javascript
复制
ionViewDidLoad() {

    this.chats = this.chatService.listChats(userId);

  }

要在模板上显示聊天记录:

代码语言:javascript
复制
<ion-list no-line>
    <button ion-item *ngFor="let chat of chats | async" (click)="onOpenChat(chat)">
      <h2>{{chat.title}}</h2>
    </button>
  </ion-list>
EN

回答 1

Stack Overflow用户

发布于 2018-02-01 18:35:57

使用.valueChanges()订阅可观察对象。

代码语言:javascript
复制
ionViewDidLoad() {

    this.chats = this.chatService.listChats(userId).valueChanges();

}

尝试这些更改

代码语言:javascript
复制
listChats(userId: string): Observable<any> {
return this.afs.collection(this.CHATS_COLLECTION)
  .snapshotChanges()
  .map((c) => {
    return c
      .filter((chat) => chat.payload.doc.id.startsWith(userId) || chat.payload.doc.id.endsWith(userId))
      .map(c => {
        const chatData = c.payload.doc.data() as Chat;
        const chatId = c.payload.doc.id;

        //the id here comes like "id1 + id2"
        let ids: string[] = c.payload.doc.id.split(this.SEPARADOR);

        //get the id 
        let id =  ids[0];

        //check in firebase the user row
        return this.afs.doc("users/" + id)
            .snapshotChanges()
            .map((c) =>{
                const data = c.payload.data() as User;
                let name = data.name;

                //Here I need to get the user name and set on the chat data
                chatData.title = name;

                return { chatId, ...chatData };

            });

      });

});}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48553590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档