//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 UTC。"],[],[],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 });"]]