Janbango
Janbango

Reputation: 191

Firebase return a collection as an array

I am trying to build an app using Firebase as the back end and need help with what seems like a simple problem. I want to return a list of nested data (I know I shouldn't be nesting data!) as an array. To try and solve this I have the following to give me a collection of the status for each record in the database:

    var ref = new Firebase("https://firebase_URL");
ref.orderByChild("Status").on("child_added", function (snapshot) {
    var articleID = snapshot.key();
    var articleData = snapshot.val();
    console.log(articleData.status.Status);

This gives me a collection of the Status but how do i combine these to console.log the list as an array?

Upvotes: 0

Views: 4465

Answers (1)

Marvin Medeiros
Marvin Medeiros

Reputation: 252

You can declare a array outside the scope of the firebase event, and on the event you push to it:

var ref = new Firebase("https://firebase_URL");
var statusCollection = []; //Array outside the 'child_added' scope
ref.orderByChild("Status").on("child_added", function (snapshot) {
    var articleID = snapshot.key();
    var articleData = snapshot.val();
    statusCollection.push(articleData.status.Status);
    //Console shows the full array collection
    console.log(statusCollection)
});

Upvotes: 3

Related Questions