muzi jack muzi jack - 4 months ago 14
AngularJS Question

How I can read an item through keys in firebase?

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"
}
}

Answer

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
              });
          }

        });

Comments