firebase collection snapshot code example

Example 1: firebase snapshot to array

function snapshotToArray(snapshot) {
    var returnArr = [];

    snapshot.forEach(function(childSnapshot) {
        var item = childSnapshot.val();
        item.key = childSnapshot.key;

        returnArr.push(item);
    });

    return returnArr;
};

Example 2: firebase snapshot to array

var ref = firebase.database().ref().child('/scenes/' + projId).orderByChild('wordcount');
ref.once('value',function(snap) {
    snap.forEach(function(item) {
        var itemVal = item.val();
        keys.push(itemVal);
    });
    for (i=0; i < keys.length; i++) {
        counts.push(keys[i].wordcount);
    }   
});

Tags:

Misc Example