简体   繁体   English

如何使用 Cloud Functions 计划对我的 Firebase Firestore 数据库中的集合进行批量更新?

[英]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.

相关问题 使用 Firebase Cloud Functions,我可以从外部 API 安排更新以更新 Firestore - Using Firebase Cloud Functions, can I schedule updates from an external API to update Firestore Firestore:如何更新 email 并将其作为单个批处理操作存储在我的 Firestore 集合中? - Firestore: How do I update email and store it in my firestore collection as a single batch operation? 如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase with Firestore? 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? 如何使用 Firebase 云功能更新 Firestore 文档的值 - How to update value of a firestore document with a firebase cloud functions 如何批量删除 Firebase 云 function 中的 Firestore 文档 - How to do a Batch delete Firestore Documents in a Firebase cloud function 如何使用云功能返回 firestore 中的每个集合及其文档? - How do I return each collection and its documents in firestore with cloud functions? 如何使用 javascript 删除具有云功能的 Firestore 集合 - How to delete a Firestore collection with cloud functions using javascript 如何使用模块 Web SDK 的版本 9 在 Cloud Firestore 集合中的文档中获取文档和嵌套集合? - How do I get document and nested collection in a document in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM