0

I am trying to read the value from firebase in my ionic app using the angularjs framework. I have the existing getPlayer function below that works fine for returning the object stored for a specific user. I am able to read key value pairs within that object.

// Data Service
    .factory('dataService', ['$firebase', 'config', function ($firebase, config) {

        // Get Firebase reference
        var dataRef = new Firebase(config.firebaseUrl);

        // Get the Firebase object
        return $firebase(dataRef);

    }]);

Here is the getPlayer function that returns an object fine.

    var users = dataService.$child('users');

    var getPlayer = function(playerId) {
      return users.$child(playerId);
    };

I am trying to implement firebase security rules so I don't want users to get all the information of a user using the above function. I wrote the firebase rules to allow getting the specific key 'fullname' in a player's object under users. I tested the authorization rules in the firebase simulator to query /users/{playerid}/fullname and it works fine.

Here is the relevant firebase db structure from the security rules

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        "friends": {".read": "auth != null"},
        "fullname": {".read": "auth != null"}
      }
    }
  }
}

I then try the below function which only returns {"$id:"fullname"}

var getPlayerFullname = function(playerId) {
  console.log(users.$child(playerId).$child('fullname'));
  return users.$child(playerId).$child('fullname');
};

When I inspect the console log in chrome I see the following which shows the value I am looking for in the '$value' key.

enter image description here

calilonghorn
  • 83
  • 10
  • and where exactly are you getting `undefined`? because if your log is printing the object then your function is returning it. – adolfosrs Jun 19 '16 at 16:13
  • Hi adolfosrs. My mistake. I updated the post. The function only returns {"$id:"fullname"} which is object in the console log but I want it to return the value in the key "$value". If I try console.log(users.$child(playerId).$child('fullname').$value) then I get undefined. – calilonghorn Jun 19 '16 at 16:46
  • great. and what is `users`? where are you getting this from? – adolfosrs Jun 19 '16 at 16:58
  • You cannot use Firebase security rules to filter the data that is returned for a specific user. Either the user has read permission on the top-level location and all the data under there is returned, or the user doesn't have read permission on the top-level location and the entire read fails. See the section on [rules are not filter](https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters) in the Firebase documentation or [this question](http://stackoverflow.com/a/14298525/209103). I can't really figure out if this is what you're looking for though. – Frank van Puffelen Jun 19 '16 at 17:14
  • Hi Frank. I am not trying to use Firebase security rules to filter my data. In my firebase security rules posted above I am giving read access to authenticated users to the "/users/{uid}/fullname" location. My goal is to get the value under /users/{uid}/fullname which is a string. – calilonghorn Jun 19 '16 at 17:20

0 Answers0