wmash
wmash

Reputation: 4202

Get all keys from table in firebase

This is the structure of the users table in Firebase:

users
 |
 +- 12345
       |
       + firstName: .....
       + lastName: .....
 |
 +- 67890
       |
       + firstName: .....
       + lastName: .....

What I want to do is collect all the keys within the users table and store them in an array. For example, on return the array would be like: [ 12345, 67890 ]

What I have so far:

var getAllUsersOrderByKey = function() {
    var deferred = $q.defer(),
        userRef = firebase.database().ref('users/').orderByKey();

    userRef.once("value").then(function(snapshot) {
        deferred.resolve(snapshot);
    });
    return deferred.promise;
}

What's called back is just the object of current user that is logged.

I already have fucntions setup that provide the relevant data when a member key is passed in (i.e. firstName)

What I am trying to do, is to get all of the keys within the users table into an array. Loop through that array and get the details of each user and push that data into another array so I can do stuff with it.

I apologise if this is a really straight forward question and that I am missing something in the docs

Upvotes: 3

Views: 2008

Answers (1)

wmash
wmash

Reputation: 4202

Solved it!

userRef was not being reset. Because all of my firebase methods are isolated in services, I hav ethe variable userRef set globally. Usually at the start of every function I reset userRef but in this instance I was setting userRef = userRef.orderByKey() which was obviously getting the last set value of userRef which is not what I wanted.

Upvotes: 1

Related Questions