//Getthe`name`attributeofthePubSubmessageJSONbody.letname=null;try{name=message.json.name;}catch(e){functions.logger.error('PubSub message was not JSON',e);}
您可能會發現 Message.attributes 中沒有訊息 ID 或訊息發布時間等基本資料。如要解決這個問題,您可以在觸發事件的 EventContext 中存取這些詳細資料。例如:
exports.myFunction=functions.pubsub.topic('topic1').onPublish((message,context)=>{console.log('The function was triggered at ',context.timestamp);console.log('The unique ID for the event is',context.eventId);});
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-09 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nGoogle Cloud's [Pub/Sub](https://cloud.google.com/pubsub/docs/) is a\nglobally distributed message bus that automatically scales as you need it. You\ncan create a function that handles Pub/Sub events by using\n[`functions.pubsub`](/docs/reference/functions/firebase-functions.pubsub).\n\nTrigger a pub/sub function\n\nYou can trigger a function whenever a new Pub/Sub message is sent\nto a specific topic. You must specify the Pub/Sub topic name that\nyou want to trigger your function, and set the event within the\n[`onPublish()`](/docs/reference/functions/firebase-functions.pubsub.topicbuilder#pubsubtopicbuilderonpublish)\nevent handler:\n\n\u003cbr /\u003e\n\n```gdscript\nexports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) =\u003e {\n // ...\n});\n```\n\n\u003cbr /\u003e\n\nAccess the pub/sub message payload\n\nThe payload for the Pub/Sub message is accessible from the\n[`Message`](/docs/reference/functions/firebase-functions.pubsub.message) object returned\nto your function. For messages with JSON in the Pub/Sub message\nbody, the Firebase SDK for Cloud Functions has a helper property to decode the message. For\nexample, here is a message published with a simple JSON payload: \n\n gcloud pubsub topics publish topic-name --message '{\"name\":\"Xenia\"}'\n\nYou can access a JSON data payload like this via the\n[`json`](/docs/reference/functions/firebase-functions.pubsub.message#pubsubmessagejson) property: \n\n```mysql\n // Get the `name` attribute of the PubSub message JSON body.\n let name = null;\n try {\n name = message.json.name;\n } catch (e) {\n functions.logger.error('PubSub message was not JSON', e);\n }https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L46-L52\n```\n\nOther, non-JSON payloads are contained in the Pub/Sub message as\nbase64 encoded strings in the message object. To read a message like the\nfollowing, you must decode the base64 encoded string as shown: \n\n gcloud pubsub topics publish topic-name --message 'MyMessage'\n\n\u003cbr /\u003e\n\n```gdscript\n// Decode the PubSub Message body.\nconst messageBody = message.data ? Buffer.from(message.data, 'base64').toString() : null;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L31-L32\n```\n\n\u003cbr /\u003e\n\nAccess message attributes\n\nPub/Sub message can be sent with data attributes set in the\npublish command. For example, you could publish a message with a `name`\nattribute: \n\n gcloud pubsub topics publish topic-name --attribute name=Xenia\n\nYou can read such attributes from\n[`Message.attributes`](/docs/reference/functions/firebase-functions.pubsub#pubsubmessageattributes):\n\n\u003cbr /\u003e\n\n```mysql\n// Get the `name` attribute of the message.\nconst name = message.attributes.name;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L65-L66\n```\n\n\u003cbr /\u003e\n\nYou might notice that some basic data such as the message ID or the\nmessage publish time are not available in `Message.attributes`. To work around\nthis, you can access these details in the triggering event's\n[`EventContext`](/docs/reference/functions/firebase-functions.eventcontext).\nFor example: \n\n exports.myFunction = functions.pubsub.topic('topic1').onPublish((message, context) =\u003e {\n console.log('The function was triggered at ', context.timestamp);\n console.log('The unique ID for the event is', context.eventId);\n });"]]