0

There may be something very obvious I'm missing here, but I cannot get my Angularjs service to return a list of users from my Firebase datastore.

This is not a question of how to generically return a response from an ajax call, but more about the appropriate use of how to handle Firebase's Promises within an Angularjs service

.service('userService', function() {
    var data; //initialize data variable
    this.getUsers = function() {
        var ref = firebase.database().ref('/users/');
        ref.once('value').then(function(snapshot) {
            users = snapshot.val(); //returns a list of user objects
        }).then(function(){
            data = users //this populates data with all the user objects
        }, function(error){
            alert('error:  ' + error);
        });
    }
    return data; //data is undefined here

}]);

I suspect it has to do with then() function but I've been banging my head on this for way too long - any ideas?

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Clay Banks
  • 4,483
  • 15
  • 71
  • 143

1 Answers1

0
ref.once('value').then(function(snapshot) {
            users = snapshot.val(); //returns a list of user objects
            return users;
        }).then(function(){
            data = users //this populates data with all the user objects
        }, function(error){
            alert('error:  ' + error);
        });

I think, you should reutrn users to process the next then().

Manish Singh
  • 518
  • 3
  • 13