Angularfire2 reading and Ionic/ Firebase reading data

Hello,

After trying all the ways and reading all pages about this, I’ve finally come here to ask for help.

I am using firebase to read the count of messages of total and the ones sent form a user then i can get to the number of unread messages and show as a badge to the user.

I have managed to loop over the chats and sum the total with the code below, but i can’t get the data with a specific user, i would liket o ask for an advice in how could i loop thru the results and get the user value?

Thanks in advance,

  checkMessages(){
    this.contaTotal = 0;
    this.countmsgs = this.af.list('/countmsg/', { preserveSnapshot: true });
    this.countmsgs.first().subscribe(snapshots => { snapshots.forEach(snapshot => {
      this.calculateSum(snapshot.val().countTotal);
       console.log(snapshot.key ,snapshot.val());
     });
   });
 }

   calculateSum(value) {
     this.contaTotal = this.contaTotal + parseInt(value);
     console.log('totalzao' + this.contaTotal);
   }

Firebase scheme:

fb

You know a user ID and you want to find a specific user? Just use Array.filter()

userdata = userArray.filter(user => user.id = idWeWantToFind);

Hi Aaron,

Thanks for the quick response, yes i do have the user which will be the this.auth.getLoggedUID().

Knowing the user i need to loop into the items for each countUser that this user is with something similar to this:

snapshot.val().countUser.FwmVBjWbBMOXXuNdxcwCm1Y8hg03 ( That will vary depending on the logged user)

and then sum the amount of messages minus the total to get the total of unread messages in all chats that this user belongs to, hopefully i’ve managed to explain better know.

Thanks again.

You want to check every single chat created by anyone ever, to search for whether a particular user is a participant? I might not understand correctly, but I don’t see how that scales.

I want to loop thru the messages and identify which one the user is associated and get the value(number of messages sent by the user), for two reasons:

  • 1 - Show like Whatsapp the unread messages for each chat.
  • 2 - Show on a badge on tab’s a total of unread messages.

Thanks again.

Create an array or map with the uuid of each chat the user participates in. When the user creates or enter a chat, you add the uuid to that field. Then you can store that array in Firebase, indexed on uuid, and make a request there, instead of having to crawl all chats created by everybody.