删除旧数据时,以前运行的Firebase功能现在失败

时间:2018-10-25 16:02:36

标签: firebase google-cloud-functions

我有一个以前可以使用的Firebase函数,但是今天部署它时,我收到一条错误消息,内容如下:

This request would cause too many functions to be triggered.

它失败,并且不会删除数据。知道为什么吗?

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// Cut off time. Child nodes older than this will be deleted.
const CUT_OFF_TIME = 180 * 24 * 60 * 60 * 1000; // 180 Days in milliseconds.

/**
 * This database triggered function will check for child nodes that are older than the
 * cut-off time. Each child needs to have a `timestamp` attribute.
 */
exports.deleteOldItems = functions.database.ref('/messages/{pushId}')
    .onWrite(event => {
      const ref = event.data.ref.parent; // reference to the items
      const now = Date.now();
      const cutoff = now - CUT_OFF_TIME;
      const oldItemsQuery = ref.orderByChild('time').endAt(cutoff);
      return oldItemsQuery.once('value').then(snapshot => {
        // create a map with all children that need to be removed
        const updates = {};
        snapshot.forEach(child => {
          updates[child.key] = null;
        });
        // execute all updates in one go and return the result to end the function
        return ref.update(updates);
      });
    });

1 个答案:

答案 0 :(得分:0)

我可以通过将触发器从OnWrite更改为OnCreate来解决此问题。我猜OnWrite(因为它处理删除,更改和添加)被脚本执行的所有删除操作调用了太多次。