firebase snapshot as 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

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

Tags:

Misc Example