firebase snapshot to array 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

firebase.database().ref('/posts').on('value', function(snapshot) {
    console.log(snapshotToArray(snapshot));
});

Example 3: firebase snapshot to array

key: string | null;

Example 4: firebase snapshot to array

firebase.database().ref('/posts').on('value', function(snapshot) {
    console.log(snapshot.val());
});

Example 5: 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: