0

I am executing the following Firebase query:

var getAdmin = function() {
    var rootRef = new Firebase(FIREBASE_URI),
        adminList = [],
        deferred = $q.defer();

    rootRef.child('user').orderByChild('role').startAt(21).once('child_added',  function (administrator) {
        // Add each Administrator to Admin List
        adminList.push(administrator);
    });

    // deferred.resolve(?)

    return deferred.promise;
};

I want to resolve the promise with the list of Administrators. How do I detect when the Firebase snapshot has been fully executed?

Lindauson
  • 2,963
  • 1
  • 30
  • 33
  • Possible duplicate of [how to discard initial data in a Firebase DB](http://stackoverflow.com/questions/19883736/how-to-discard-initial-data-in-a-firebase-db) – Kato Oct 08 '15 at 16:24

1 Answers1

1

Firebase is a realtime platform, so a list is never 'fully executed'.

If you just want that part of the list at that instant in time, you should use .once('value', callback).

rootRef.child('user').orderByChild('role').startAt(21).once('value', function(list) {

    // Add each Administrator to Admin List
    list.forEach(function(adminSnap) {
        adminList.push(adminSnap.val());
    });

    deferred.resolve(adminList)
});

return deferred.promise;
Anid Monsur
  • 4,538
  • 1
  • 17
  • 24