2016-06-11 2 views
0

나는 firebase 3에서 데이터를 얻으려고 시도하고 있었고 내가 만든 오류를 알고 있는지 궁금해하고있었습니다.firebase에서 데이터 가져 오기 3

서비스 :

.service("searchService", function($firebaseArray, fb) { 

    var database = firebase.database(); 
    var bookId = firebase.auth().currentUser; 

    this.getPostedBooks = function() { 
     return database.ref("postedBooks/" + bookId).once("value"); 
    } 

컨트롤러 :

$scope.getPostedBooks = function() { 
     searchService.getPostedBooks().then(function(response) { 
     $timeout(function() { 
      console.log(response.val()) 
     }, 5000); 

     }) 

내가 데이터가 늦게오고 있는지 확인하기 위해 타임 아웃을 사용하려고하지만, 무슨 일이 있어도, 난 그냥 널 얻을.

답변

0

당신은 $ q를 서비스

https://docs.angularjs.org/api/ng/service/ $ q를

.service("searchService", function($firebaseArray, $q, fb) { 
    var database = firebase.database(); 
    var bookId = firebase.auth().currentUser; 
    var dfd = $q.defer(); 

    return { 
     getPostedBooks : function() { 
     return database.ref("postedBooks/" + bookId).once("value", function(snapshot) { 
      dfd.resolve(snapshot.val()); 
     }, function (errorObject) { 
      //log error 
     }); 
     return dfd.promise; 
     } 
    } 
}) 
을 시도 할 수
관련 문제