0

Hi guys so I have this data: enter image description here When I console.log(Object.entries(data)) I get this: https://pastebin.com/jPyRuxFX

I always know that in this object is my userID: lwcIQTcpAae4e38hrD2K5Ar76W93 members object and messages object. I want to filter it so there would be only other user data. Another userID could be any unique key It' is in object but I don't have it in any variable. So I need to filter out all data. This is what I tried:

 getChats = _userId => {
let data;
let usersData = [];
var readedData = firebase
  .database()
  .ref('chats')
  .orderByChild('members/' + _userId)
  .equalTo(true);
readedData.once('value', snapshot => {
  data = snapshot.val();

  const temp = { ...data };
  const filtered = Object.entries(data).map(duom =>
    duom
      .filter(user => user !== firebase.auth().currentUser.uid)
      .filter(user => !user.members)
      .filter(user => !user.messages),
  );

  console.log('filtered data: ' + JSON.stringify(data[filtered[0][0]]));

  this.setState({ chats: data, usersData: usersData });
  return true;
});

};

But result is this : [["-M6cy1JNy1V5cs35StW-"],["-M4O-aIxt9w2iKuCDweN"],["-M4NzlagjmeFH7IR_Api"]] What I do wrong, how to eddit this filter?

The result I want is in each object:

"tempuser":{ "username":"Egle", "profile_picture":"https://scontent.fkun1-1.fna.fbcdn.net/v/t1.0-9/38612482_1935470283165234_1771800590876147712_n.jpg?_nc_cat=111&_nc_sid=85a577&_nc_ohc=3rVHabbNJ3kAX9_0FBq&_nc_ht=scontent.fkun1-1.fna&oh=4277fff9a1441726bbf0efe18b44fae0&oe=5E9AA1F7" }

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Emilis
  • 152
  • 1
  • 4
  • 21
  • Your current data structure makes it easy to find the members for a chat room. It does not however make it easy to find the chat rooms for a user. To allow the latter, you'll need to add an additional data structure `member_chats/$uid: { chatid1: true, chatid2: true }` See my longer explanation here: https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen May 06 '20 at 14:37
  • To find chat rooms for a user i use this:.orderByChild('members/' + _userId) .equalTo(true); and it works good so far. Do you know how could i filter the data? – Emilis May 06 '20 at 18:24
  • Your current code either requires that you define an index for each UID, or it pulls down all data from all `chats` to perform the query. I explain that in more detail in my answer in the link in the section on "You can only index known properties" – Frank van Puffelen May 06 '20 at 22:37

1 Answers1

0

So I managed to filter the data like this:

const filtered = Object.entries(data).map(([key, value]) =>
        Object.keys(value)
          .filter(value => value !== _userId)
          .filter(value => value !== 'members')
          .filter(value => value !== 'messages'),
      );
Emilis
  • 152
  • 1
  • 4
  • 21