Я использую облачные функции для удаления узлов через 2 часа на firebase. Однако, когда я добавляю узел, он удаляется, как только он создается внутри базы данных
Мой index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const CUT_OFF_TIME = 2 * 60 * 60 * 1000; // 2 Hours in milliseconds
exports.deleteOldItems = functions.database.ref('/posts/{randomID1}/{randomID2}/timestamp')
.onWrite(function(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);
return oldItemsQuery.once('value').then(function(snapshot) {
// create a map with all children that need to be removed
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});
Моя база данных:
"posts" : {
"randomID1" : {
"randomID2" : {
"timestamp" : 1557842159
}
},
"randomID3" : {
"randomID4" : {
"timestamp" : 1557842203
}
},