2016-11-16 75 views
0

可能有一些非常明显的我在这里失踪,但我无法让我的Angularjs服务从我的Firebase数据存储返回用户列表。Angularjs服务不会从Firebase返回数据

这不是如何一般返回从AJAX调用响应的问题,但更多的关于适当使用如何在Angularjs服务中处理火力地堡的承诺

.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 

}]); 

我怀疑它做与then()函数,但我一直在这方面我的头撞了太久 - 任何想法?

回答

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); 
     }); 

我觉得,你应该让用户处理下一个然后()。