I am new to JS and Cloud Functions and I would like to perform an update to a collection in my Firestore database every day at midnight. I have a collection appointmentTimes with a boolean field available and I want to reset this to true every day at midnight. So far I've tried using the following:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.resetAppointmentTimes = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
const appointmentTimesCollectionRef = db.database().collection('appointmentTimes');
appointmentTimesCollectionRef.get().then(querySnapshot => {
if (querySnapshot.empty) {
return null;
} else {
let batch = db.database().batch();
querySnapshot.forEach(doc => {
batch.update(doc.ref, { available: true });
});
return batch.commit();
}
}).catch(error => { console.log(error); });
})
Thank you for any input/suggestions!
db? And what isdb.database()? - Renaud Tarnec