I am trying to make this Cloud Function to work. I would like to delete a document from Firestore that has as name the previous date of function execution. This function is supposed to be executed every night at 2am, but for some reason it is not working. Here is my code:
exports.deleteYesterday = functions.pubsub
.schedule("0 02 * * *")
.onRun(async (context) => {
var yesterday = new Date();
var dd = String(yesterday.getDate() - 1).padStart(2, "0");
var mm = String(yesterday.getMonth() + 1).padStart(2, "0");
var yyyy = yesterday.getFullYear();
const dateString = (yyyy + "-" + mm + "-" + dd).toString();
let ref = admin.firestore().collection("citas").doc(dateString);
return ref.delete();
});
When logging dateString value I get the correct date format I need: dateString
This is how the database looks like: documents
Thanks!!