0

enter image description here

I have a collection called chats, I want to listen on the document for changes in the msg while also returning the data from the msg field, I have come up with a query but I am not getting the result I want. below is the code.

 getCurrentMsgs(chatId){
   return this.firestore.collection('chats', ref => ref.where('id', '==', chatId))
   .snapshotChanges()
    .pipe(
      map(actions => {
        return actions.map(a => {
          // eslint-disable-next-line @typescript-eslint/ban-types
          const data: Object = a.payload.doc.data();
          const id = a.payload.doc.id;
          return { id, ...data };
        }
      );
    })
  );
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user15317824
  • 370
  • 6
  • 15

1 Answers1

0

The snapshot received from listeners will always contains data of that whole document. You'll have to compare existing document data and the newly received update to get the changes in array. Packages like deep-object-diff might be useful in this case.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • so how do i focus on just the `msg` field? – user15317824 Apr 03 '22 at 12:55
  • @user15317824 you can try using `deep-object-diff` where you can pass existing data and new data. It should return whatever has changed in the document., – Dharmaraj Apr 03 '22 at 12:57
  • thanks, i am not looking for the difference, i just want to get the the current data and display it. – user15317824 Apr 03 '22 at 12:59
  • @user15317824 can you explain what you are looking for and what is not working in your code above in that case? – Dharmaraj Apr 03 '22 at 12:59
  • basically, i want to have an observable that provides me with the most current data in the meg field . – user15317824 Apr 03 '22 at 13:01
  • @user15317824 you just want to return messages instead of `{ id, ...data }`? Might best best to share a screenshot of what you get right now vs what you want to. – Dharmaraj Apr 03 '22 at 13:03