【问题标题】:Cloud Functions : how to change dynamically a Cloud Firestore triggerCloud Functions:如何动态更改 Cloud Firestore 触发器
【发布时间】:2020-02-04 22:54:11
【问题描述】:

我的应用使用 Firestore 集合“users/{userId}/alarms/2019/10”,其中 2019 是当前年份,10 是当前月份。我使用了一个 Cloud Firestore 触发器,该触发器在此集合发生更改时被调用。

在下月初(11月),我想每个月监控“users/{userId}/alarms/2019/11/{alarm}”,等等。 我尝试了使用“firebase deploy --only 函数”部署的以下代码。

exports.changeMonthAlarmsListener = functions.pubsub.schedule('0 0 1 * *').onRun((context) => {
    const currentDate= new Date();
    const year = currentDate.getFullYear();
    const month = currentDate.getMonth()+1;
    console.log('new Firestore Cloud trigger for users/{user}/alarms/%d/%d/{alarm}', year, month);

    exports.monthAlarmsListener = functions.firestore
    .document('users/{user}/alarms/'+year+'/'+month+'/{alarm}')
    .onWrite((change, context) => {
        console.log('user %s: change for alarm %s', context.params.user, context.params.alarm);
    });

    return true;
});

我从 Cloud 控制台执行了 changeMonthAlarmsListener。我观察到跟踪“用户/{user}/alarms/2019/10/{alarm} 的新 Firestore Cloud 触发器”。然后我在 Firestore 集合“users/a_user/2019/10”中创建了一个文档“foo”,但似乎从未执行过 monthAlarmsListener,因为没有跟踪“user a_user: change for alarm foo”。

我想动态定义一个新的 Cloud 函数并不像我这样简单地声明它。但是如何进行呢?我在 Cloud Functions 的文档中没有找到任何内容。

【问题讨论】:

    标签: google-cloud-firestore google-cloud-functions


    【解决方案1】:

    不可能从另一个函数的执行中动态部署一个函数。如果您想要另一个函数,则必须静态定义它并使用 CLI 部署它。如果您确实需要针对日期部分的各种组合使用不同的功能,听起来您最终会为每个可能的组合创建一个新函数,或者只是用一个函数来决定每次调用每个组合时如何处理它。

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 2018-10-16
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2019-07-12
      • 2021-11-09
      相关资源
      最近更新 更多