0

I've been trying to handle properly in my angular service an explicit synchronization with Firebase. I use angularFireCollection with initial load callback function (I need to preload additional data basing on the data returned by the first query). However, I don't know how should I access the fetched data in the callback function:

getGroupIds: function() {

var deferred = $q.defer();
var ref = new Firebase('https://<XXX>.firebaseio.com/groups');
angularFireCollection(ref, function(groups) {

    console.log(groups); 

    deferred.resolve(groups);
});

return deferred.promise;
}

In the above example, how can I access actual data from groups object?d Thanks in advance for any tips.

2
  • May be yourService.getGroupIds().then(function(groups) { }); Commented Sep 26, 2013 at 13:08
  • Yes, you are right, this is how I can get data from the resolved promise. But my question is in fact different: is it possible (and if yes, then how) I can access the data returned by Firebase inside the optional angularFireCollection callback function. Commented Sep 26, 2013 at 13:35

2 Answers 2

2

A Firebase snapshot is provided as an argument to the callback function, and you can extract the value from it as follows:

angularFireCollection(ref, function(snapshot) {
  console.log(snapshot.name() + " has value " + snapshot.val());
});
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I was looking for. Thanks for that example. Still, I think that angularfire documentation should provide more details on it.
0

usually, I'm setting the collection to scope, but either of these seem to work.

var groups = angularFireCollection(ref, function() {
    console.log(groups);     
});

or in a controller

$scope.groups = angularFireCollection(ref, function() {
    console.log($scope.groups );     
});

1 Comment

In my opinion none of them should print actual values returned by Firebase, since the data is assigned after callback function is executed. Anyway, the solution provided in the answer above works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.