首页 文章

Ionic Firebase无法使用从DB获取的数据返回承诺

提问于
浏览
0

我'm new to Firebase and Ionic and currently trying to get some data from the server as a certain view is loaded. Right now I am able to get the data inside the service, but I' m无法将获取的 data 作为对控制器的承诺返回 . 这是调用服务的代码:

$scope.$on('$ionicView.beforeEnter', function () {
    var user = firebase.auth().currentUser;       
    fireBaseService.getDataFromDB(user.uid).then(function(res){
      console.log(res); 
    });

以下是调用Firebase API的服务:

this.getDataFromDB = function(uid){
    return firebase.database().ref('table/' + uid).on('value', function(snapshot) {
        var data = snapshot.val(); //data is available here
        return data;
    });
}

这是我得到的错误:

TypeError: fireBaseService.getExpensesFromDB(...).then is not a function

1 回答

  • 0
    call from controller
    fireBaseService.getDataFromDB(user.uid, function(res) {
      console.log(res);
    }
    
    service call
    getDataFromDB = function(uid, callback){
        return firebase.database().ref('table/' + uid).on('value', function(snapshot) {
            var data = snapshot.val(); //data is available here
            callback(data);
        });
    }
    

相关问题