taylorm
taylorm

Reputation: 55

Get the value of a key after filtering with value in Firebase

I am new to Firebase. I am trying to get the value of a key after filtering it with a value. I first get the specific user with the room_num value "323" by using

uref.orderByChild("room_num").equalTo(room_num).once("value" ...

After I get the object of this user, I want to have access to his/her telephone number. But the code below does not give me what I need.

var uref = new Firebase('https://XXX.firebaseio.com/users');
uref.orderByChild("room_num").equalTo("323").once("value", function(data) {
      $scope.telephone = data.telephone;
      console.log($scope.telephone);
});

I also tried wrapping it with $firebaseArray.

Here is what my database looks like

users
  6GFbWBV9rINa3ZCDN45BgM1PO3o2
      email: "[email protected]"
      name: "Mark"
      room_num: "323"
      telephone: "72839202"

My database looks like this

Upvotes: 0

Views: 639

Answers (1)

cartant
cartant

Reputation: 58400

The callback is passed a snapshot. The snapshot will have a child for each entry that matches the query. To access the data within the child snapshot, you need to call val():

uref.orderByChild("room_num").equalTo("323").once("value", function (snapshot) {
  snapshot.forEach(function (childSnapshot) {
    var data = childSnapshot.val();
    $scope.telephone = data.telephone;
    console.log($scope.telephone);
  });
});

Upvotes: 1

Related Questions