Retrieve data from firebase with promise

Hi everybody

I have a Application where I try to include Firebase. I got it work so far but the way I included the get request in my controller my script just continue without waiting for the data. I would like to convert my request into a request with a promise.

something similar as I used before with normal $http requests:

$http(http_req).then(function mySucces(data) {
// Success
}, function myError(data) {
//something was wrong
});

My current controller looks like this:

[code]
.controller(ā€˜NoticiasCtrlā€™, function(FURL, $scope, $state, $http, $ionicLoading, $rootScope, $firebaseObject, $firebaseArray) {

$scope.newslist = [];

$scope.doRefresh = function() {

    //Show loading comment
	$ionicLoading.show({
    template: '<p>Carregando notĆ­cias...</p><ion-spinner></ion-spinner>'
	});

    var ref = firebase.database().ref('news');
    $scope.newslist = $firebaseArray(ref);

    $ionicLoading.hide();
    $scope.$broadcast('scroll.refreshComplete');

	};

$scope.doRefresh();

})[/code]

Basically I want to have the line ā€œvar ref = firebaseā€¦ā€ with a promise. And if the data is received continue with $scopeā€¦

Thank you in advance

firebase.database().ref('news')
  .then ( function (ref) {
        $scope.newslist = $firebaseArray(ref);
        $ionicLoading.hide();
        $scope.$broadcast('scroll.refreshComplete')
  });
1 Like

Thank you for your help @A_Burgess, but unfortunately this is not working.
I posted your code and I get error ā€œionic.bundle.js:25642 TypeError: firebase.database(ā€¦).ref(ā€¦).then is not a functionā€

Any other idea?

Otherwise I could invent some workaround and just donā€™t continue until the array is not empty :frowning: but I guess there has to be a better solution.

Perhaps this helps more, I donā€™t even know why it syntax errors and I never tries. Sorry.

Unfortunately there is nothing about my question to find. (Besides your link is wrong and goes to the deprecated documentation). The current docs are https://firebase.google.com/support/guides/firebase-web

I did read the docs up and down, and it is also working as described in the docs but these are not any actions with promises. In there examples everything works like in my example but what I donā€™t get done is a proper error handling and/or get it work so the script does wait for firebase before it continues.

What I am looking for is a way to receive data from firebase BUT wait for the result before I continue AND catch errors if any happen. (like missing data, no connection etc.)

It looks pretty much the same to me. Looking at the link you posted will not really help you, look at angularfire docsā€¦

ok understood, I check it out and let you know later ā€¦ thx for your help