Firebase的云功能:枚举DataSnapshot属性

时间:2022-10-29 12:57:12

I'd like to iterate over DataSnapshot properties in my Firebase function. Here's my code.

我想在Firebase函数中迭代DataSnapshot属性。这是我的代码。

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 => {
            ...
        })
    })

It doesn't seem like memberIds is enumerable because I get the error:

看起来似乎memberIds不可枚举,因为我得到错误:

memberIds.forEach is not a function.

memberIds.forEach不是一个函数。

1 个解决方案

#1


4  

memberIds will be an Object - not an Array - so you cannot enumerate it using forEach. You can, however, access it as a snapshot using child:

memberIds将是一个Object - 而不是一个Array - 因此您无法使用forEach枚举它。但是,您可以使用子项将其作为快照访问:

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

#1


4  

memberIds will be an Object - not an Array - so you cannot enumerate it using forEach. You can, however, access it as a snapshot using child:

memberIds将是一个Object - 而不是一个Array - 因此您无法使用forEach枚举它。但是,您可以使用子项将其作为快照访问:

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