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 benullif the event was triggered by an unauthenticated action or admin SDK.context.authType: Specifies the type of authentication associated with the event. Possible values includeADMIN(triggered by admin operations),USER(triggered by a logged-in user), orUNAUTHENTICATED(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 dynamicidvalue viacontext.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 DatabaseonWritetriggers, this will look something likeproviders/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 likeval()to get the raw data,exists()to check if data existed, orchild(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 asdata.beforeto 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
相关产品推荐
相关产品推荐

