I'm trying to access a simple Firebase database (https://blazing-fire-6348.firebaseio.com) and store its values separately, but they're not storing properly. The code below spits out all 10's, rather than the values stored in Firebase. I don't think my issue is just scope, but Firebase syntax. The tilde's are the website url, stackoverflow just wouldn't let me post the link multiple times.
Thanks for any help you can provide.
var a = 10; var b = 10; var c = 10;
var dataRef = new Firebase("~~~/a");
dataRef.on('value', function(snapshot) {
a = snapshot.val();
// alert(a);
});
var dataRef = new Firebase("~~~/b");
dataRef.on('value', function(snapshot) { if(snapshot.val() != null){
b = snapshot.val();
// alert(b);
} });
var dataRef = new Firebase("~~~/c");
dataRef.on('value', function(snapshot) { if(snapshot.val() != null){
c = snapshot.val();
// alert(c);
} });
alert(a); alert(b); alert(c);`
`
alerts at the bottom show, you can access the variables anywhere. But they won't have the value from remote Firebase, until after thedataRef.on('value'callback fires. It comes from the asynchronous nature of AJAX calls. Given that the behavior you are seeing is completely expected, maybe you can describe in another way what you are trying to accomplish.