0

I have two "tables" that are USER and CONGE. In the table "CONGE" I inserted the id of the users. However I do not know how to display the user's leave according to their id.

I would like to post the "Congé" according to id.

{
  "conge" : {
    "-LaW8hzv8O3PSlZk6OPIJU" : {
      "Mouth" : "January",
      "id_user" : "LaW_9iIrm_-tiDpWEE7"
    }
  },
  "user" : {
    "-LaW_9iIrm_-tiDpWEE7" : {
      "email" : "asdfasdf",
      "name" : "Michel",
      "prenom" : "adadfas"
    },
    "-LaWfQpaRfludVkzAPvq" : {
      "email" : "dup@gmail",
      "name" : "Michel",
      "prenom" : "dupuis"
    },
    "-LaWfTTF-Lifs79bGbhy" : {
      "email" : "JC@gmail",
      "name" : "Jean ",
      "prenom" : "Louzis"
    }
  }
}

Code :

`
  var rootRef = firebase.database().ref().child("user");

  rootRef.on("child_added",function(snap){
    var name = snap.child("name").val();
    var email = snap.child("email").val();
    var prenom = snap.child("prenom").val();


$("#table_body").append("<tr><td>"+name+"</td><td>"+prenom+"</td><td>"+email+"</td></tr>");
    // $("#table_body").append("<tr><td>"+name+"</td><td>"+email+"</td><td><button id='rem' onclick='remove()'>Remove</button></td></tr>");
  });

function remove(){


  // console.log(firebase.database().ref().child('user').push().key);
  rootRef.remove();
location.reload();
}

function add(){

  console.log(name);
  alert("COOL");
}`

Display

Mikimouche
  • 41
  • 1
  • 1
  • 5
  • Migo, Can you please help us by adding the Javascript Code from the image to the question ?? Also, the Database data can be exported as JSON and you can paste it into the question as well. It would be easy for us to copy paste code and find the issue you are having !! – Fire-In-D-Hole Mar 21 '19 at 19:01
  • Yes, You can copy paste the Code(Javascript, HTML) you have tried so far and add it to your question by editing it. Also the Data from your Realtime Database you can export it to JSON format. [Click me to see how to Export](https://stackoverflow.com/a/47182298/10953546) – Fire-In-D-Hole Mar 21 '19 at 19:13

1 Answers1

1

In NoSQL world, it is very common to duplicate data in order to easily write your queries (you will find a lot of "literature" on this subject on the web).

With your current database structure you would need to issue two queries in order to get the desired data.

If you duplicate the data as follows, you will get the info with one query:

{
  "conge" : {
    "-LaW8hzv8O3PSlZk6OPIJU" : {
      "Mouth" : "January",
      "id_user" : "LaW_9iIrm_-tiDpWEE7"
    }
  },
  "user" : {
    "-LaW_9iIrm_-tiDpWEE7" : {
      "email" : "asdfasdf",
      "name" : "Michel",
      "prenom" : "adadfas",
      "conges" : {
              "January" : true,
              "April" : true
      }
    },
    "-LaWfQpaRfludVkzAPvq" : {
      "email" : "dup@gmail",
      "name" : "Michel",
      "prenom" : "dupuis",
      "conges" : {
              "March" : true,
              "April" : true,
              "October" : true
      }
    },
    "-LaWfTTF-Lifs79bGbhy" : {
      "email" : "JC@gmail",
      "name" : "Jean ",
      "prenom" : "Louzis"
    }
  }
}

The only drawback with this method is that you have to keep the two nodes (i.e. conge and user) in sync. But this can easily be done by writing to the two database nodes simultaneously, using 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