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

Firebase Cloud Functions v1.0版本中数据包含哪些字段?

Firebase Cloud Functions v1.0: Fields for Non-HTTP Triggers

Hey there! Let's break down the key fields available in Firebase Cloud Functions v1.0 for non-HTTP triggers (like the Realtime Database onWrite example you shared). We'll cover both the context and data objects since those are the core data carriers in these functions.

Context Object Fields

The context parameter holds metadata about the event that triggered your function. From your code example, here's what each field does:

  • context.auth: Contains authentication details for the user who triggered the event (if applicable). This includes data like the user's UID and token claims—note it will be null if the event was triggered by an unauthenticated action or admin SDK.
  • context.authType: Specifies the type of authentication associated with the event. Possible values include ADMIN (triggered by admin operations), USER (triggered by a logged-in user), or UNAUTHENTICATED (triggered by an unauthenticated user).
  • context.params: Stores values for any wildcard parameters you defined in your trigger path. For example, if your path is /pat/with/{id}, you can access the dynamic id value via context.params.id.
  • context.eventId: A unique identifier for the triggered event—each event gets its own distinct ID, which is useful for debugging or idempotency checks.
  • context.timestamp: An ISO-formatted string representing when the event was triggered (e.g., 2024-05-20T12:34:56.789Z).
  • context.eventType: A string that identifies the exact type of event that triggered the function. For Realtime Database onWrite triggers, this will look something like providers/google.firebase.database/eventTypes/ref.write.
  • context.resource: The full resource path of the database location that triggered the event, pointing directly to the data that changed.

Data Object Fields

The data parameter (specifically for Realtime Database onWrite triggers) gives you access to the state of the data before and after the change:

  • data.before: A snapshot of the data at the trigger path before the write occurred. You can use methods like val() to get the raw data, exists() to check if data existed, or child(path) to access nested values.
  • data.after: A snapshot of the data at the trigger path after the write occurred. It supports the same methods as data.before to inspect the updated state.

Example Code Breakdown

Here's your provided code, formatted for clarity, showing how to access these fields:

exports.dbWrite = functions.database.ref('/pat/with/{id}').onWrite((data, context) => { 
  const authVar = context.auth; 
  const authType = context.authType; 
  const pathId = context.params.id; 
  const eventId = context.eventId; 
  const timestamp = context.timestamp; 
  const eventType = context.eventType; 
  const resource = context.resource; 
});

内容的提问来源于stack exchange,提问作者Ally Makongo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:07:53