Reputation:
I have the following data structure:
Unfortunately, 'days' does not get removed from the database with the code below.
My current code:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}') //irrelevant to the question
.onWrite(event => {
var ref = event.data.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value', 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);
}).then(function() {;
return functions.database.ref('/days').remove(); // /days doesn't get removed!
});
});
Upvotes: 1
Views: 623
Reputation: 12813
You could do this:
let updates = {};
updates['/days'] = null;
firebase.database().ref().update(updates);
See "Updating or deleting data" in the docs;
In your code:
.then(function() {;
let updates = {};
updates['/days'] = null;
return firebase.database().ref().update(updates)
});
Edit: Try something like this:
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const defaultDatabase = admin.database();
...
let updates = {};
updates['/days'] = null;
defaultDatabase.ref().update(updates);
Ref: https://firebase.google.com/docs/database/admin/start
Upvotes: 2