2

I'm trying to read all data from my firebase but I canno do this

Here is my code:

firebase.database().ref().child('users/').on('value',function(snapshot)                   {
  window.alert(snapshot.val());
});

and my firebase look like this:

"users":    
{

  "uid": {

    "username": "AAA",
    "email": "aaa@a.com"
  },

  "uid": {

    "username": "ZZZ",
    "email": "zzz@z.com"
  }

}

I can't get anything data using this code

update

I have defined my security in firebase

{
 "rules":{

  "users":{

    "$uid": {
      // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
      ".write": "auth !== null && auth.uid === $uid ",      
      //".write": "auth !== null && (auth.uid === $uid || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')",

      // grants read access to any user who is logged in with an email and password
      ".read": "auth !== null &&  auth.uid ===$uid "
      //".read": "auth !== null &&  (auth.uid ===$uid  || auth.uid ==='0127a226-83ec-4eae-bbc7-53b81da10c0d')"

      //".validate": "newData.hasChildren(['full_name','uid','state_light'])"   
  }
}

} }

Jay
  • 34,438
  • 18
  • 52
  • 81
kinfo
  • 21
  • 3
  • See my answer and if that doesn't fix it, how is your initial firebase reference defined? – Jay May 29 '16 at 14:17
  • A new Firebase Database created on console.firebase.google.com nowadays requires a user to be authenticated before they can read data. Most likely you don't have permission to read the data. A quick way to see if that is the cause, is to add a error handler to your `on()` call: `on('value', ..., function(error) { console.error(error); }` and check the JavaScript console of your browser. – Frank van Puffelen May 29 '16 at 14:26
  • it show error and i post my security in thread – kinfo May 29 '16 at 14:50
  • Can you remove your rules and set .read and .write both to true under the main rules node? That will verify if the rules are causing the issue. – Jay May 29 '16 at 15:06
  • i think not the rules problem, because i have app using the same rules, but it can read/write correctly – kinfo May 29 '16 at 15:27
  • Rules are not filters: https://stackoverflow.com/a/14298525/6680611 – cartant Jun 20 '17 at 17:54
  • Possible duplicate of [Restricting child/field access with security rules](https://stackoverflow.com/questions/14296625/restricting-child-field-access-with-security-rules) – cartant Jun 20 '17 at 17:54

1 Answers1

-1

Remove the extraneous / from .child('users/') and format it like so

firebase.database().ref('users').on('value', function(snapshot) {
  window.alert(snapshot.val());
});

Also, in your firebase structure, you can't have two keys with the same name (uid).

Jay
  • 34,438
  • 18
  • 52
  • 81
  • still cannot read data oh the uid just the example i am using firebase tutorial to defined firebase and i add a code var rootRef = firebase.database().ref(); – kinfo May 29 '16 at 14:45