Ранее работающая функция Firebase теперь не работает при удалении старых данных - PullRequest
0 голосов
/ 25 октября 2018

У меня есть функция 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 голосов
/ 25 октября 2018

Мне удалось исправить это, изменив триггер с OnWrite на OnCreate.Я предполагаю, что OnWrite (так как он имеет дело с удалениями, изменениями и добавлениями) вызывался слишком много раз всеми удалениями, которые делал скрипт.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...