2

This is my attempt but it returns undefined but I've figured this can't be done without having the keys as .child(keys) or something of that nature.

var ref = new Firebase("https://users.firebaseio.com/");
ref.child("users").child(authData.uid).on("value", function(snapshot) {
  var users = snapshot.val();
  console.log("snap: " + snapshot.val());
  console.log("username: " + users.username);
  console.log("username: " + users.email);
});

Here's my firebase json text. I want to read email lastname and username through many keys not just per key.

"users": {
  "6179eb29-9691-4828-be5a-28a94e84703b": {
    "-KLWoGzjymD0Sgvf5Rcs": {
      "email": "jaycarter@gmail.com",
      "lastname": "cater",
      "username": "jay"
    }
  },
  "6a210785-0a73-4c83-b3e1-d3828f9d8a3b": {
    "-KLqMiLTB71dTh7PrVpL": {
      "email": "k@gmail.com",
      "lastname": "john",
      "username": "smith"
    }
  }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Muzi Jack
  • 788
  • 1
  • 7
  • 20

3 Answers3

0

You haven't defined your firebase project anywhere. I think you must use like this .

<script>
// Initialize Firebase
var config = {
  apiKey: "your_api_key",
  authDomain: "project-0000000000000000.firebaseapp.com",
  databaseURL: "https://project-0000000000000000.firebaseio.com",
  storageBucket: "project-0000000000000000.appspot.com",
};
firebase.initializeApp(config);
var ref= firebase.database().ref();
ref.child("users").child(authData.uid).on("value", function(snapshot) {
var users = snapshot.val();
console.log("snap: " + snapshot.val());
console.log("username: " + users.username);
console.log("username: " + users.email);
});
</script>
Pritish Joshi
  • 2,025
  • 3
  • 18
  • 33
  • i actually wanted to find out if this could be done how to read through child('keys') on ref.child("users").child(authData.uid).on("value", function(snapshot) – Muzi Jack Jul 07 '16 at 09:24
  • Not clear to me... Can you please explain a bit more..? – Pritish Joshi Jul 07 '16 at 09:26
  • is it possible to read through name from ref.child("users").child(authData.uid).child('name'); without having to provide key value pairs like .child("-KLWoGzjymD0Sgvf5Rcs") when calling retrieving a name. – Muzi Jack Jul 07 '16 at 09:35
  • according to your need you can get that object just using this _ref.child("users").child("6179eb29-9691-4828-be5a-28a94e84703b");_ – Pritish Joshi Jul 07 '16 at 09:50
0

You are looking for orderByChild combined with equalTo.

ref.child("users").orderByChild("username").equalTo("smith").on("value", function(snapshot) {
  var users = snapshot.val();
  console.log("snap: " + snapshot.val());
  //loop over the found results
  snapshot.forEach(function(user){
      console.log(user);
  });
});

In your snippet you are initializing your ref using the legacy sdk. New firebase sdk has changed it a little bit.

firebase.initializeApp(config);
var ref= firebase.database().ref();

Also, keep in mind that you wont be able to use multiple orderByChild to query for multiple child parameters. In this case, this question will give you some ideias on how to work on it.

Community
  • 1
  • 1
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
0

taskref.onAuth(function(authData) {
      taskref
        .child("users")
        .child(authData.uid)
        .child("task")
        .orderByChild('date')
        .on('value', function(snapshot) {
          $scope.list = [];
          snapshot.forEach(function(child) {
            //  $scope.list = $scope.list.concat(child.val());
            $scope.list.splice(0, 0, child.val());
            console.log($scope.list);
          });

          $scope.taskKeys = function() {
            return Object.keys($scope.list);
          }

          $scope.complete = function(key) {
            taskref.child("users")
              .child(authData.uid)
              .child('task')
              .child(key)
              .set({
                status: true
              });
          }

        });
Muzi Jack
  • 788
  • 1
  • 7
  • 20
  • i answered this question, so by using "Object.keys" so that i can be able to pass the specific key to my function then pass to my child node. – Muzi Jack Jul 27 '16 at 13:27