0

I want to get a value outside of a function in firebase to use globally, but it is not working.

var ref = new Firebase("https://xxxxx.firebaseio.com/users/xxxx/roles/admin/"); // true or false

    ref.once("value", function(snapshot) {
    console.log("here");
    console.log(snapshot.val());
    var isAdmin = snapshot.val();
    return isAdmin;
});

console.log(isAdmin); /// cannot get value
Jason
  • 931
  • 2
  • 12
  • 26
  • Also see: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call, which has a detailed explanation of why this can't work. – Frank van Puffelen Feb 24 '16 at 19:23

1 Answers1

1

You cannot use isAdmin outside, because it is not available, use it in callback

ref.once("value", function(snapshot) {
    //You should wrap you code here, to use isAdmin variable
    //You can do whatever with isAdmin, but you can't return it.
    //It's callback that running asynchronously
    console.log(isAdmin)
})
isvforall
  • 8,768
  • 6
  • 35
  • 50
  • So, it is impossible to use "isAdmin" globally or outside of the function? Do I need to wrap the rest of my code within this function? – Jason Feb 24 '16 at 16:08