【问题标题】:Is there a way to have a firebase trigger which is triggered on updating multiple subcollections?有没有办法让firebase触发器在更新多个子集合时触发?
【发布时间】:2021-07-13 00:13:06
【问题描述】:

我一直试图在 firebase 中获取多子集合级别结构的参数和数据。我需要知道每个级别的所有 documentId。我正在使用 onCreate 触发器。

我的结构如下:

utilities/gas/paymentTypes/card/dates/2020-01

并且一直在尝试获取以下参数:

  • 实用程序类型,例如气体
  • 付款类型,例如卡
  • 日期,例如2020-01

我一直在尝试使用的代码如下:

export const addDistributionArea = functions.firestore.document("utilsTypesDistributionArea/{utilityType}/paymentTypes/{paymentType}/dates/{date}")
    .onCreate(async (snapshot: any, context: any) => {  
        console.log("params", context.params);
        console.log("id", snapshot.ref.id);
        console.log("data", snapshot.data());
        return null;

});

目前我可以检索实用程序类型和支付类型,但是 {date} 始终未定义。

结果:

>  params { utilityType: 'gas', paymentType: 'card', date: undefined }
>  id QDD_1
>  data {}

谁能帮我找到解决办法。

【问题讨论】:

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


    【解决方案1】:

    没有像这样跨多次写入触发的云函数。

    我能想到的选项:

    1. 将所有数据写入单个文档,然后根据需要在 Cloud Function 代码中将数据散列到其他集合。

    2. 在每个文档上触发,然后检测您的 Cloud Function 代码中是否存在所有必要的文档。如果没有,请忽略此触发器并等待其他文档出现。

    其中任何一个都可以工作,尽管我个人更喜欢第一个,因为我喜欢将组合文档视为应用程序在这种情况下自动发布的“交易数据”。

    【讨论】:

    • 感谢您的建议,我觉得我同意第一个选项更可取,并且在某些方面更简洁。
    猜你喜欢
    • 2012-03-02
    • 2018-02-02
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多