Облачные функции Firebase не удаляют старые узлы, как ожидалось - PullRequest
1 голос
/ 06 июня 2019

Я использую облачные функции Firebase для удаления старых узлов в базе данных Firebase.Почти 1 месяц назад я задал аналогичный вопрос , в котором проблема заключалась в том, что значения были в секундах, а не в миллисекундах, но проблема сохраняется.Как решить эту проблему?

Мой index.js:

'use strict';

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

// Cut off time. Child nodes older than this will be deleted.
const CUT_OFF_TIME = 30000; // 30 seconds 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('/posts/{id1}/{id2}/timestamp').onWrite(async (change) => {
  var ref = change.after.ref.parent; // reference to the parent
  var now = Date.now();
  var cutoff = now - CUT_OFF_TIME;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  var snapshot = await oldItemsQuery.once('value');
  // create a map with all children that need to be removed
  var 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);
});

и моя база данных struture

  "posts" : {
    "38d1d0aa-d774-4a69-a78e-44d02b285669" : {
      "-LggzAxeAYXF7tOav_vF" : {
        "timestamp" : 1559827888988
      }
    },
    "58fe50ae-db93-4a22-996f-7a28d82583ba" : {
      "-Lggze2bvHZx2gJeM8OA" : {
        "timestamp" : 1559828012239
      }
    }
  },
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...