0

I have a problem with my Firebase DB. I have the following structure:

{
  "adminroles":{

       "admin1": "some_uuid"
    }
}

In my file.js, I use the following code:

return firebase.database()
         .ref('adminrole/')
         .once('value')
         .then(function(snapshot) {
           var adminuser = snapshot.child('admin1').val();
         });

However, the snapshot is undefined.

Could some one please help me? Is a problem with my database structure?

AL.
  • 36,815
  • 10
  • 142
  • 281
Api
  • 11
  • 5
  • Please fix your grammar and formatting – JonyD Feb 05 '17 at 13:34
  • 1
    Thanks for your comment @jonyD. I fix the formatting and grammar. – Api Feb 05 '17 at 13:59
  • Try removing the '/' from 'adminrole/' and reference just 'adminrole'. You might be referencing non-existing node with such expression. Rest seems ok to me. I would need more information about the structure of the database to figure out more. – jankoritak Feb 05 '17 at 14:13
  • @Api check this page. see the nodejs example tab. Try to do it in steps and using the chrome dev tools debug – JonyD Feb 05 '17 at 14:14
  • @Api in this code `snapshot` is defined in the `then` callback. More likely your problem exists when you call the function that contains this code, in which case I recommend studying [this answer](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). But please share the [minimal, *complete* code that reproduces the problem](http://stackoverflow.com/help/mcve), which would include how you invoke the snippet you shared and what exact error message you get and on which line you get it. – Frank van Puffelen Feb 05 '17 at 15:51
  • Thanks for all your comments they help me to find the problem. I will share as the answer to the question (It was in fact a stupid problem). – Api Feb 06 '17 at 09:47

1 Answers1

0

I found the problem by passing also an error callback to the Then() of the promise. In fact the problem was in my rules of the database.

I had:

    "adminrole":{
         "admin1":{
            ".read": "auth != null &&  auth.uid == 'some_uid'",
            ".write": "auth != null &&  auth.uid == 'some_uid'"
             }
       }

So the once() returned an error because I didn't have read access to "adminrole". I change the rule as follows and it works now.

    "adminrole":{
       ".read": "auth != null",
       ".write": "auth != null &&  auth.uid == 'some_uid'"
      }

The function is as follows:

return firebase.database()
     .ref('adminrole')
     .once('value')
     .then(function(snapshot) {
       var adminuser = snapshot.child('admin1').val();
     }, function(error) {
        var errorMessage = error.message;
        alert(errorMessage);
        console.log(error);
     });
Api
  • 11
  • 5