[英]How do I schedule a batch update to a collection in my Firebase Firestore database using Cloud Functions?
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.我是 JS 和 Cloud Functions 的新手,我想每天午夜对我的 Firestore 数据库中的集合执行更新。 I have a collection appointmentTimes
with a boolean field available
and I want to reset this to true
every day at midnight.我有一个集合appointmentTimes
时间,其中有一个available
的 boolean 字段,我想每天午夜将其重置为true
。 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!感谢您的任何意见/建议!
It is not clear what is db.database()
.目前尚不清楚什么是db.database()
。 You should use the Admin SDK and call admin.firestore()
to get a Firebase App instance.您应该使用 Admin SDK 并调用admin.firestore()
来获取 Firebase App 实例。 also, you need to return the Promises chain (watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/ for more details).此外,您需要返回 Promises 链(观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频: https://firebase.google.com/docs/functions/video-series/了解更多详细信息)。
The following should do the trick:以下应该可以解决问题:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.resetAppointmentTimes = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
const appointmentTimesCollectionRef = db.collection('appointmentTimes');
return appointmentTimesCollectionRef.get() // See the return here
.then(querySnapshot => {
if (querySnapshot.empty) {
return null;
} else {
let batch = db.batch();
querySnapshot.forEach(doc => {
batch.update(doc.ref, { available: true });
});
return batch.commit();
}
})
.catch(error => {
console.log(error);
return null;
});
})
You can use node-schedule like a cron file您可以像使用 cron 文件一样使用节点调度
var schedule = require('node-schedule');
var j = schedule.scheduleJob('0 0 0 * * *', function(){
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); });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.