Cloud Functions for Firebase: Listing DataSnapshot Properties

I would like to iterate over the DataSnapshot properties in my Firebase function. Here is my code.

alignmentsRef.once('value')
.then(function(snapshot) {
    snapshot.forEach(function(k) {
        var itemId = k.key //itemId
        var childData = k.val() //{downvotes: {memberId: "down"}, upvotes: {memberId: "up"}}
        var memberIds = childData.downvotes // {memberId: "down"}
        memberIds.forEach(l => {
            ...
        })
    })

We seem to memberIdslist, because I get the error:

memberIds.forEach is not a function.

+4
source share
1 answer

memberIdswill be Object- not a Array- so you cannot list it with forEach. However, you can access it as a snapshot using child:

alignmentsRef
  .once('value')
  .then(function (snapshot) {
    snapshot.forEach(function (k) {
        k.child('downvotes').forEach(function (d) {
          console.log(`${d.key} = ${d.val()}`);
        });
    });
+4
source

Source: https://habr.com/ru/post/1672880/


All Articles