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