Firebase云功能在forEach循环中更改另一个节点的数据

时间:2018-03-16 19:25:29

标签: javascript node.js firebase google-cloud-functions

我正在尝试从我的云功能上的每个循环中的节点中删除项目。但它总是只看第一项。我想循环所有项目。这是我的代码:

snapshot.forEach(function(child) {
       console.log('all items');
       return thisAppDb.ref('userRemindMatches/'+child.key+'/'+compId).once('value').then(snapshot => {

            if (snapshot.exists()) {
                snapshot.ref.remove();
                console.log('match removed from this user : '+child.key);
            }
            return thisAppDb.ref('user : '+child.key+' matches removed');

        });
    });

正如你在这里看到的,在snapshot.foreach中,我正在调用另一个ref。节点然后我想从该节点中删除项目。它只工作一次。我怎样才能为每件物品循环呢? (在我看来,它是因为循环返回而发生的)

2 个答案:

答案 0 :(得分:0)

改变这个:

&

到此:

 return thisAppDb.ref('userRemindMatches/'+child.key+'/'+compId).once('value').then(snapshot => {

答案 1 :(得分:0)

documentation for forEach清楚地说明了传递函数的返回值是如何解释的:

  

function(非null admin.database.DataSnapshot)

     

将为每个子DataSnapshot调用的函数。的的   callback可以返回true以取消进一步的枚举。

你正在返回一个“truthy”值,告诉每个人停止。