Reputation: 187
I'm trying to delete old entries in my realtime database with this function. I'm not able to get the old records to delete, any ideas? The function seems to be running fine. Code Below..
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/{pushId}')
.onWrite((change, context) => {
var ref = change.after.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 600;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value', function(snapshot) {
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null
});
return ref.update(updates);
});
});
The database is structured like so...
-'db-name'
- posts
- post 1
- post 2
- etc, etc
Upvotes: 0
Views: 69
Reputation: 317342
You can't use both the promise and the callback of once(). You're supposed to choose one or the other. It's typically better to choose to use the promise.
return oldItemsQuery.once('value').then(snapshot => {
var updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
return ref.update(updates);
});
Upvotes: 1