-1

I am trying to access to a object into another object in my firebase database, i have a structure like this:

enter image description here

I want to get all the objects that have the email that i send by parameters, i am using .child to access to the childs into my object but i am not success with the query, this is my code

$ ref_db.child("/groups").child("members").orderByChild("email").equalTo(email).once("value", (snapshot)=>{
      console.log(snapshot.val());
    }); 

The snapshot.val() always is undefined.

could you help me with the query?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • In addition to the answers from Peter and Renaud, also have a read of my answers [here](https://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value) and [here](https://stackoverflow.com/questions/41527058/many-to-many-relationship-in-firebase). – Frank van Puffelen Apr 29 '18 at 13:42

2 Answers2

1

One efficient way to get "all the groups that have that email inside the members object" would be to denormalize you data and have another "main node" in your database where you store all "members" (i.e. their email) and the "groups" they belong to.

This means that each time you add a "member" node under a "group" (including its email) you will also add the group as a child of the member email, in this other "main node".

More concretely, here is how would be the database structure:

Your current structure:

- groups
  - -LB9o....
   ...
   - members
     - -LB9qbd....
       -email: xxxx@zzz.com
     - -LBA7R....
       -email: yyyyy@aaaa.com

And the extra structure:

- groupsByMembers
   - xxxxxx@zzzcom
     - Grupo1: true
   - yyyyy@aaaacom
     - Grupo1: true
     - Grupo2: true
   - bbbcccc@dddcom
     - Grupo6: true
     - Grupo8: true

Note that in the "extra structure" the dots within an email address are removed, since you cannot include a point in a node id. You will have to remove them accordingly when writing and querying.

This way you can easily query for the list of groups a member is belonging to, as shown below. Without the need to loop several times over several items. This dernomalization technique is quite classic in NoSQL databases.

    const mailToSearchFor = xxxx.xx@zzz.com;
    const ref = database.ref('/groupsByMembers/' + mailToSearchFor.replace(/\./g, ''));

    ref.once('value', snapshot => {
        const val = snapshot.val();
        for (let key in val) {
            if (val.hasOwnProperty(key)) {
                console.log(key);
            }
        }
    });

In order to write to the two database nodes simultaneously, use the update method as explained here https://firebase.google.com/docs/database/web/read-and-write#update_specific_fields

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

This is because you have a random key before members, you need to go through the path and not skip a node, to be able to access the values:

ref_db.child("groups").child("-LB9oWcnE0wXx8PbH4D").child("members").orderByChild("email").equalTo(email).once("value", (snapshot)=>{ 
console.log(snapshot.val()); 
});
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thank you Peter! it works! But what if i want to get all the groups that have that email inside the members object? do you know how to do that? – Carlos Giovani Barillas Colón Apr 29 '18 at 08:44
  • put the snapshot at child `groups`, then loop inside `groups` using `forEach`, then get the value of `members` and loop inside of that also using `forEach` and get the emails – Peter Haddad Apr 29 '18 at 09:02