I've been having several issues with my Firebase scheduled function. I'm reviewing the logs and documentation and it's running and logs as "Success" and OK, but the desired nodes of my Realtime Database are not updating/deleting. Here is my index.js code...
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunction = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
const ref = admin.database().ref('messages/{pushId}');
var now = Date.now();
var cutoff = now - 24 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
return oldItemsQuery.once('value')
.then(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);
});
});
Here is how I've structured the data in my database reference that I want deleted at "cutoff"...
I've updated my firebase functions to the latest version. The only other issue that may be causing this is a Warning that I should
Consider adding ".indexOn": "timeStamp" at /messages/{pushId} to your security rules for better performance.
Since this was a warning and the function worked fine when it was .onWrite I'm not sure this is it.