2016-11-16 7 views
0

私はここで紛失していますが、Firebaseのデータストアからユーザーリストを返すためのAngularjsサービスを取得できません。AngularjsサービスがFirebaseからデータを返さない

これは、一般的にAJAX呼び出しからの応答を返す方法の質問はありませんが、私はそれが関係している疑いがある

.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 

}]); 

Angularjsサービス内Firebaseの約束をどのように扱うかの適切な使用についての詳細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); 
     }); 

私は、次のthen()を処理するユーザーをreutrnする必要があります。

関連する問題