Firebase query if child of child contains a value











up vote
35
down vote

favorite
20












The structure of the table is:




  • chats

  • --> randomId

  • -->--> participants

  • -->-->--> 0: 'name1'

  • -->-->--> 1: 'name2'

  • -->--> chatItems


etc



What I am trying to do is query the chats table to find all the chats that hold a participant by a passed in username string.



Here is what I have so far:



 subscribeChats(username: string) {
return this.af.database.list('chats', {
query: {
orderByChild: 'participants',
equalTo: username, // How to check if participants contain username
}
});
}









share|improve this question




























    up vote
    35
    down vote

    favorite
    20












    The structure of the table is:




    • chats

    • --> randomId

    • -->--> participants

    • -->-->--> 0: 'name1'

    • -->-->--> 1: 'name2'

    • -->--> chatItems


    etc



    What I am trying to do is query the chats table to find all the chats that hold a participant by a passed in username string.



    Here is what I have so far:



     subscribeChats(username: string) {
    return this.af.database.list('chats', {
    query: {
    orderByChild: 'participants',
    equalTo: username, // How to check if participants contain username
    }
    });
    }









    share|improve this question


























      up vote
      35
      down vote

      favorite
      20









      up vote
      35
      down vote

      favorite
      20






      20





      The structure of the table is:




      • chats

      • --> randomId

      • -->--> participants

      • -->-->--> 0: 'name1'

      • -->-->--> 1: 'name2'

      • -->--> chatItems


      etc



      What I am trying to do is query the chats table to find all the chats that hold a participant by a passed in username string.



      Here is what I have so far:



       subscribeChats(username: string) {
      return this.af.database.list('chats', {
      query: {
      orderByChild: 'participants',
      equalTo: username, // How to check if participants contain username
      }
      });
      }









      share|improve this question















      The structure of the table is:




      • chats

      • --> randomId

      • -->--> participants

      • -->-->--> 0: 'name1'

      • -->-->--> 1: 'name2'

      • -->--> chatItems


      etc



      What I am trying to do is query the chats table to find all the chats that hold a participant by a passed in username string.



      Here is what I have so far:



       subscribeChats(username: string) {
      return this.af.database.list('chats', {
      query: {
      orderByChild: 'participants',
      equalTo: username, // How to check if participants contain username
      }
      });
      }






      angular firebase firebase-realtime-database angularfire2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '16 at 14:11









      Frank van Puffelen

      220k25361387




      220k25361387










      asked Nov 17 '16 at 13:46









      John

      205136




      205136
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          58
          down vote



          accepted










          Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.



          A few problems here:




          • you're storing a set as an array

          • you can only index on fixed paths


          Set vs array



          A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:



          participants: ["puf", "puf"]


          That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.



          My rule of thumb: if you find yourself writing array.contains(), you should be using a set.



          A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:



          participants: {
          "puf": true
          }


          The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:



          participants: {
          "puf": true
          }


          And when you'd join:



          participants: {
          "john": true,
          "puf": true
          }


          This is the most direct representation of your requirement: a collection that can only contain each participant once.



          You can only index known properties



          With the above structure, you could query for chats that you are in with:



          ref.child("chats").orderByChild("participants/john").equalTo(true)


          The problem is that this require than you define an index on `participants/john":



          {
          "rules": {
          "chats": {
          "$chatid": {
          "participants": {
          ".indexOn": ["john", "puf"]
          }
          }
          }
          }
          }


          This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.



          Invert the index - pull categories up, flattening the tree



          Second rule of thumb: model your data to reflect what you show in your app.



          Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:



          userChatrooms: {
          john: {
          chatRoom1: true,
          chatRoom2: true
          },
          puf: {
          chatRoom1: true,
          chatRoom3: true
          }
          }


          Now you can simply determine your list of chat rooms with:



          ref.child("userChatrooms").child("john")


          And then loop over the keys to get each room.



          You'll like have two relevant lists in your app:




          • the list of chat rooms for a specific user

          • the list of participants in a specific chat room


          In that case you'll also have both lists in the database.



          chatroomUsers
          chatroom1
          user1: true
          user2: true
          chatroom2
          user1: true
          user3: true
          userChatrooms
          user1:
          chatroom1: true
          chatroom2: true
          user2:
          chatroom1: true
          user2:
          chatroom2: true


          I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.



          Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.



          Cloud Firestore



          This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.






          share|improve this answer























          • This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
            – lusus_vir
            Dec 16 '17 at 23:14










          • @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
            – damirstuhec
            Feb 6 at 11:34










          • it solved my similar issue, thanks
            – Sitecore Sam
            Jun 20 at 9:25











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40656589%2ffirebase-query-if-child-of-child-contains-a-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          58
          down vote



          accepted










          Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.



          A few problems here:




          • you're storing a set as an array

          • you can only index on fixed paths


          Set vs array



          A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:



          participants: ["puf", "puf"]


          That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.



          My rule of thumb: if you find yourself writing array.contains(), you should be using a set.



          A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:



          participants: {
          "puf": true
          }


          The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:



          participants: {
          "puf": true
          }


          And when you'd join:



          participants: {
          "john": true,
          "puf": true
          }


          This is the most direct representation of your requirement: a collection that can only contain each participant once.



          You can only index known properties



          With the above structure, you could query for chats that you are in with:



          ref.child("chats").orderByChild("participants/john").equalTo(true)


          The problem is that this require than you define an index on `participants/john":



          {
          "rules": {
          "chats": {
          "$chatid": {
          "participants": {
          ".indexOn": ["john", "puf"]
          }
          }
          }
          }
          }


          This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.



          Invert the index - pull categories up, flattening the tree



          Second rule of thumb: model your data to reflect what you show in your app.



          Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:



          userChatrooms: {
          john: {
          chatRoom1: true,
          chatRoom2: true
          },
          puf: {
          chatRoom1: true,
          chatRoom3: true
          }
          }


          Now you can simply determine your list of chat rooms with:



          ref.child("userChatrooms").child("john")


          And then loop over the keys to get each room.



          You'll like have two relevant lists in your app:




          • the list of chat rooms for a specific user

          • the list of participants in a specific chat room


          In that case you'll also have both lists in the database.



          chatroomUsers
          chatroom1
          user1: true
          user2: true
          chatroom2
          user1: true
          user3: true
          userChatrooms
          user1:
          chatroom1: true
          chatroom2: true
          user2:
          chatroom1: true
          user2:
          chatroom2: true


          I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.



          Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.



          Cloud Firestore



          This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.






          share|improve this answer























          • This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
            – lusus_vir
            Dec 16 '17 at 23:14










          • @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
            – damirstuhec
            Feb 6 at 11:34










          • it solved my similar issue, thanks
            – Sitecore Sam
            Jun 20 at 9:25















          up vote
          58
          down vote



          accepted










          Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.



          A few problems here:




          • you're storing a set as an array

          • you can only index on fixed paths


          Set vs array



          A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:



          participants: ["puf", "puf"]


          That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.



          My rule of thumb: if you find yourself writing array.contains(), you should be using a set.



          A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:



          participants: {
          "puf": true
          }


          The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:



          participants: {
          "puf": true
          }


          And when you'd join:



          participants: {
          "john": true,
          "puf": true
          }


          This is the most direct representation of your requirement: a collection that can only contain each participant once.



          You can only index known properties



          With the above structure, you could query for chats that you are in with:



          ref.child("chats").orderByChild("participants/john").equalTo(true)


          The problem is that this require than you define an index on `participants/john":



          {
          "rules": {
          "chats": {
          "$chatid": {
          "participants": {
          ".indexOn": ["john", "puf"]
          }
          }
          }
          }
          }


          This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.



          Invert the index - pull categories up, flattening the tree



          Second rule of thumb: model your data to reflect what you show in your app.



          Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:



          userChatrooms: {
          john: {
          chatRoom1: true,
          chatRoom2: true
          },
          puf: {
          chatRoom1: true,
          chatRoom3: true
          }
          }


          Now you can simply determine your list of chat rooms with:



          ref.child("userChatrooms").child("john")


          And then loop over the keys to get each room.



          You'll like have two relevant lists in your app:




          • the list of chat rooms for a specific user

          • the list of participants in a specific chat room


          In that case you'll also have both lists in the database.



          chatroomUsers
          chatroom1
          user1: true
          user2: true
          chatroom2
          user1: true
          user3: true
          userChatrooms
          user1:
          chatroom1: true
          chatroom2: true
          user2:
          chatroom1: true
          user2:
          chatroom2: true


          I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.



          Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.



          Cloud Firestore



          This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.






          share|improve this answer























          • This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
            – lusus_vir
            Dec 16 '17 at 23:14










          • @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
            – damirstuhec
            Feb 6 at 11:34










          • it solved my similar issue, thanks
            – Sitecore Sam
            Jun 20 at 9:25













          up vote
          58
          down vote



          accepted







          up vote
          58
          down vote



          accepted






          Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.



          A few problems here:




          • you're storing a set as an array

          • you can only index on fixed paths


          Set vs array



          A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:



          participants: ["puf", "puf"]


          That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.



          My rule of thumb: if you find yourself writing array.contains(), you should be using a set.



          A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:



          participants: {
          "puf": true
          }


          The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:



          participants: {
          "puf": true
          }


          And when you'd join:



          participants: {
          "john": true,
          "puf": true
          }


          This is the most direct representation of your requirement: a collection that can only contain each participant once.



          You can only index known properties



          With the above structure, you could query for chats that you are in with:



          ref.child("chats").orderByChild("participants/john").equalTo(true)


          The problem is that this require than you define an index on `participants/john":



          {
          "rules": {
          "chats": {
          "$chatid": {
          "participants": {
          ".indexOn": ["john", "puf"]
          }
          }
          }
          }
          }


          This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.



          Invert the index - pull categories up, flattening the tree



          Second rule of thumb: model your data to reflect what you show in your app.



          Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:



          userChatrooms: {
          john: {
          chatRoom1: true,
          chatRoom2: true
          },
          puf: {
          chatRoom1: true,
          chatRoom3: true
          }
          }


          Now you can simply determine your list of chat rooms with:



          ref.child("userChatrooms").child("john")


          And then loop over the keys to get each room.



          You'll like have two relevant lists in your app:




          • the list of chat rooms for a specific user

          • the list of participants in a specific chat room


          In that case you'll also have both lists in the database.



          chatroomUsers
          chatroom1
          user1: true
          user2: true
          chatroom2
          user1: true
          user3: true
          userChatrooms
          user1:
          chatroom1: true
          chatroom2: true
          user2:
          chatroom1: true
          user2:
          chatroom2: true


          I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.



          Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.



          Cloud Firestore



          This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.






          share|improve this answer














          Your current data structure is great to look up the participants of a specific chat. It is however not a very good structure for looking up the inverse: the chats that a user participates in.



          A few problems here:




          • you're storing a set as an array

          • you can only index on fixed paths


          Set vs array



          A chat can have multiple participants, so you modelled this as an array. But this actually is not the ideal data structure. Likely each participant can only be in the chat once. But by using an array, I could have:



          participants: ["puf", "puf"]


          That is clearly not what you have in mind, but the data structure allows it. You can try to secure this in code and security rules, but it would be easier if you start with a data structure that implicitly matches your model better.



          My rule of thumb: if you find yourself writing array.contains(), you should be using a set.



          A set is a structure where each child can be present at most once, so it naturally protects against duplicates. In Firebase you'd model a set as:



          participants: {
          "puf": true
          }


          The true here is really just a dummy value: the important thing is that we've moved the name to the key. Now if I'd try to join this chat again, it would be a noop:



          participants: {
          "puf": true
          }


          And when you'd join:



          participants: {
          "john": true,
          "puf": true
          }


          This is the most direct representation of your requirement: a collection that can only contain each participant once.



          You can only index known properties



          With the above structure, you could query for chats that you are in with:



          ref.child("chats").orderByChild("participants/john").equalTo(true)


          The problem is that this require than you define an index on `participants/john":



          {
          "rules": {
          "chats": {
          "$chatid": {
          "participants": {
          ".indexOn": ["john", "puf"]
          }
          }
          }
          }
          }


          This will work and perform great. But now each time someone new joins the chat app, you'll need to add another index. That's clearly not a scaleable model. We'll need to change our data structure to allow the query you want.



          Invert the index - pull categories up, flattening the tree



          Second rule of thumb: model your data to reflect what you show in your app.



          Since you are looking to show a list of chat rooms for a user, store the chat rooms for each user:



          userChatrooms: {
          john: {
          chatRoom1: true,
          chatRoom2: true
          },
          puf: {
          chatRoom1: true,
          chatRoom3: true
          }
          }


          Now you can simply determine your list of chat rooms with:



          ref.child("userChatrooms").child("john")


          And then loop over the keys to get each room.



          You'll like have two relevant lists in your app:




          • the list of chat rooms for a specific user

          • the list of participants in a specific chat room


          In that case you'll also have both lists in the database.



          chatroomUsers
          chatroom1
          user1: true
          user2: true
          chatroom2
          user1: true
          user3: true
          userChatrooms
          user1:
          chatroom1: true
          chatroom2: true
          user2:
          chatroom1: true
          user2:
          chatroom2: true


          I've pulled both lists to the top-level of the tree, since Firebase recommends against nesting data.



          Having both lists is completely normal in NoSQL solutions. In the example above we'd refer to userChatrooms as the inverted index of chatroomsUsers.



          Cloud Firestore



          This is one of the cases where Cloud Firestore has better support for this type of query. Its array-contains operator allows filter documents that have a certain value in an array, while arrayRemove allows you to treat an array as a set. For more on this, see Better Arrays in Cloud Firestore.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 7 at 15:21

























          answered Nov 17 '16 at 14:27









          Frank van Puffelen

          220k25361387




          220k25361387












          • This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
            – lusus_vir
            Dec 16 '17 at 23:14










          • @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
            – damirstuhec
            Feb 6 at 11:34










          • it solved my similar issue, thanks
            – Sitecore Sam
            Jun 20 at 9:25


















          • This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
            – lusus_vir
            Dec 16 '17 at 23:14










          • @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
            – damirstuhec
            Feb 6 at 11:34










          • it solved my similar issue, thanks
            – Sitecore Sam
            Jun 20 at 9:25
















          This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
          – lusus_vir
          Dec 16 '17 at 23:14




          This is an awesome explanation. I have my data stored like this, which allows me to grab a chatroom from a user's list, or grab a user from a chat room's members. But what it doesn't allow is to observe .childChanged for the chatrooms of which I am a member. If I try to set up an observer for chats where members/myId = true, then I get the unspecified index error. How would you go about this? I have a question posted here: stackoverflow.com/questions/47769044/…
          – lusus_vir
          Dec 16 '17 at 23:14












          @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
          – damirstuhec
          Feb 6 at 11:34




          @frank-van-puffelen referencing your last code snippet, let's say I am "user1" and I fetch all keys of chatrooms I'm a member of. Then for each chatroom key I fetch the full chatroom object, and display the list of chatrooms in a list. That's good, however, what if I also needed to access full user objects that are a part of a chatroom. It seems very strange that I would have to do another for-loop (2-level nested for-loop!) fetch for every single user in every single chatroom to get that data. Is there a better way?
          – damirstuhec
          Feb 6 at 11:34












          it solved my similar issue, thanks
          – Sitecore Sam
          Jun 20 at 9:25




          it solved my similar issue, thanks
          – Sitecore Sam
          Jun 20 at 9:25


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40656589%2ffirebase-query-if-child-of-child-contains-a-value%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings