I have a ProductDatabase collection which stores all the products I'm displaying on my app. Each document in the collection has a timestamp field called 'SBD' which represents the sell by date of each item. I have created and uploaded a cloud function which checks the each item in the database every 2 hours to check that every items SBD field has not passed. This is the function:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.delete = functions
.region("europe-west2")
.pubsub.schedule("every 2 hours").onRun((context) => {
const productsRef = admin.firestore().collection("TestDatabase");
const query = productsRef.where("SBD", "<", Date.now());
return query.get()
.then((querySnapshot) => {
const batch = admin.firestore().batch();
querySnapshot.forEach((doc) => {
batch.delete(doc.ref);
});
return batch.commit();
});
});
This program uploads appears in the firebase cloud functions console and says it's being invoked every 2 hours however no products are deleted from the TestDatabase even though they should all be deleted. The index.js file is stored in a bucket in the google cloud console which has all the necessary permissions. The database and cloud function are also in the same region.