You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

第一代Firestore触发型Cloud Function测试触发事件JSON示例

为Firestore onCreate触发的第一代Cloud Function构造测试事件JSON

我有一个由Firestore文档创建事件触发的第一代Cloud Function(来自FlutterFlow),希望通过Google Cloud控制台内置的TESTING功能测试该函数。操作时需要在Configure Triggering Event框中输入JSON,但找不到对应的格式。我的函数代码如下:

exports.sendUserPushNotificationsTrigger = functions
.runWith(kPushNotificationRuntimeOpts)
.firestore.document(`${kUserPushNotificationsCollection}/{id}`)
.onCreate(async (snapshot, _) => {
  try {
    // Ignore scheduled push notifications on create
    const scheduledTime = snapshot.data().scheduled_time || "";
    if (scheduledTime) {
      return;
    }

    // Don't let user-triggered notifications to be sent to all users.
    const userRefsStr = snapshot.data().user_refs || "";
    if (userRefsStr) {
      await sendPushNotifications(snapshot);
    }
  } catch (e) {
    console.log(`Error: ${e}`);
    await snapshot.ref.update({ status: "failed", error: `${e}` });
  }
});

适配的测试事件JSON示例

针对你的Firestore onCreate触发器,测试事件需要匹配Cloud Functions接收的Firestore事件结构,同时包含你函数逻辑依赖的字段。以下是直接可用的JSON模板:

{
  "oldValue": {
    "createTime": "2024-01-01T00:00:00Z",
    "fields": {},
    "name": "projects/你的项目ID/databases/(default)/documents/你的集合ID/test-doc-id",
    "updateTime": "2024-01-01T00:00:00Z"
  },
  "updateMask": {},
  "value": {
    "createTime": "2024-01-01T00:00:00Z",
    "fields": {
      "scheduled_time": {
        "stringValue": ""
      },
      "user_refs": {
        "stringValue": "users/user1,users/user2"
      }
    },
    "name": "projects/你的项目ID/databases/(default)/documents/你的集合ID/test-doc-id",
    "updateTime": "2024-01-01T00:00:00Z"
  }
}

关键字段替换与说明

  • value.name/oldValue.name:替换为你的Google Cloud项目ID、集合ID(即代码中kUserPushNotificationsCollection的值)和任意测试文档ID。
  • value.fields:对应Firestore文档的实际数据:
    • scheduled_time.stringValue设为空字符串,确保函数不会跳过执行(匹配代码中scheduledTime为空时的逻辑)。
    • user_refs.stringValue设为非空值,触发sendPushNotifications调用(匹配代码中userRefsStr非空时的逻辑)。
  • oldValue:因是onCreate触发,旧值只需保持空字段的合规结构即可。

场景测试变种

  1. 测试定时通知跳过逻辑:将scheduled_time.stringValue设为具体时间(如"2024-12-31T23:59:59Z"),函数会直接返回,不执行推送。
  2. 测试无用户引用场景:将user_refs.stringValue设为空字符串,函数不会调用推送方法。

内容的提问来源于stack exchange,提问作者Frank van Puffelen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 12:43:15