Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

typescript - Get data sub collection and grouping by parent collection firebase angular 9

I am trying show array data from collection and filter it by sub collection data.

Here is the collection

  • User
    • id
    • name

Here for the sub-collection

  • Chat
    • idUser
    • message
    • date
    • read

How to list and grouping all user with date from subcollection (chat). The result array like this

  • id
  • name
  • message (last message)
  • read
  • date (last chat time)

do i need create new collection for store all data?

I have tring with this code. Can not show the data when looping it

getChats() {
let users: any = [];

return this.chatService.getUsers().pipe(
  switchMap((res: any) => {
    users = res;
    return this.firestore
      .collection('user')
      .doc(this.idUser)
      .collection('chat', (ref) => ref.orderBy('date'))
      .valueChanges({ idField: 'id' }) as Observable<Message[]>;
  }),
  map((messages) => {
    for (let m of messages) {
      m.name = this.getUserForMsg(m.id, users);
      m.read = this.idUser === m.dc001frommsg;
    }
    return messages;
  })
);

}

(await this.getChats()).subscribe((data) => {
  this.listChat = data;
}))

in Html

<ul *ngFor="let user of listChats">
   <li (click)="detail(user)"> {{ user.name }}</li>
</ul>
question from:https://stackoverflow.com/questions/65599120/get-data-sub-collection-and-grouping-by-parent-collection-firebase-angular-9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...