Florin G
Florin G

Reputation: 30

Firebase import sorted data

I want to import sorted data from a Firebase database.

As an example: I have a small company and I need to import the data of all employers. I am owner_01 which means I would have to import all the users whose child owner_uid match owner_01.

The data will be used on a website, which means I will be using javascript.

Here you have the JSON :

    {
  "Owner_users" : {
    "owner_01" : {
      "email" : "[email protected]",
      "uid" : 123
    },
    "owner_02" : {
      "email" : "[email protected]",
      "uid" : 12345
    }
  },
  "Users" : {
    "user_01" : {
      "Email" : "[email protected]",
      "Name" : "asd",
      "age" : 23,
      "owner_uid" : 123,
      "uid" : "asd"
    },
    "user_02" : {
      "Email" : "[email protected]",
      "Name" : "all",
      "age" : 49,
      "owner_uid" : 123,
      "uid" : "asdfg"
    },
    "user_03" : {
      "age" : 60,
      "email" : "[email protected]",
      "owner_uid" : 12345,
      "uid" : "sdfzsvsvsd"
    }
  }
}

Check this url to see how the database is structured. Update:

HTML code:

<table class="mdl-data-table mdl-js-data-table ">
    <thead>
      <tr>
        <th class="mdl-data-table__cell--non-numeric">Email</th>
        <th>Age</th>
        <th>uid</th>
      </tr>
    </thead>
    <tbody id="usersData">
    </tbody>
  </table>

JS code

var usersRef = firebase.database().ref('Users')
var query = usersRef.orderByChild('owner_uid').equalTo('123');
query.on("child_added", function(snapshot) {

  var email = snap.child("Email").val;
  var name = snap.child("Name").val;
  var uid = snap.child("uid").val;

  $("#usersData").append("<tr><td" + email + "</td><td>" + name + "</td><td>" + uid + "</td></tr>");

});

Upvotes: 0

Views: 141

Answers (1)

Mico
Mico

Reputation: 2009

var usersRef = firebase.database().ref('Users')
var query = usersRef.orderByChild('owner_uid').equalTo('YOUR UID HERE');
query.on("child_added", function(snapshot) {
  console.log(snapshot.val().uid);
});

Upvotes: 1

Related Questions