Repository navigation
Cloud Functions background sample appears incorrect: entity.data.myMessage #260
Description
Activity
Which property do you mean?
myMessageordata?event.dataexists
event.data.myMessageis just an example. Perhaps we need to be clear about howmyMessageis populated in that exampleI just tested it, seems to work fine.
The error case:
$ gcloud alpha functions call helloWorld --data '{"data":{}}' executionId: xhsrd3tpbwpe result: | Function failed, details: No message defined!Then looking at https://console.cloud.google.com/logs/viewer?project=nodejs-docs-samples&resource=cloud_function I see:
The success case:
$ gcloud alpha functions call helloWorld --data '{"data":{"myMessage":"Hello, world!"}}' executionId: xhsrgavertuoI agree though, it's not clear what the use case is for that particular sample.
But, you're cheating :-)
The code is an example of a Cloud Function being used as a background function.
There are two ways background functions should be triggered, in response to a:
-- Pub/Sub topic being published
-- Cloud Storage bucket eventIn both of those cases, the event.data schema is defined by the originating service.
For Pub/Sub,
gcloud alpha pubsub topics publish \ MY-TOPIC \ '{"myMessage":"Hello, Henry!"}' \ --project=MY-PROJECTI receive a string encoding of the JSON object (not a JSON object):
data: "{\"myMessage\":\"Test\"}"For Cloud Storage,
touch test.txt gsutil cp test.txt gs://MY-TRIGGER-BUCKETI receive:
data: ""If I deploy your function as-is, I receive an error:
Error: No message defined! at exports.originalPubSub (/user_code/index.js:31:14) at /var/tmp/worker/worker.js:572:16 at Domain.<anonymous> (/var/tmp/worker/worker.js:530:9) at Domain.run (domain.js:221:14) at Handler.handle (/var/tmp/worker/worker.js:520:7) at /var/tmp/worker/worker.js:587:13 at Layer.handle [as handle_request] (/var/tmp/worker/node_modules/express/lib/router/layer.js:82:5) at next (/var/tmp/worker/node_modules/express/lib/router/route.js:110:13) at Route.dispatch (/var/tmp/worker/node_modules/express/lib/router/route.js:91:3) at Layer.handle [as handle_request] (/var/tmp/worker/node_modules/express/lib/router/layer.js:82:5)You are right, this particular sample only works if you call it with
gcloud alpha functions call. 😕I removed this sample from the docs.
- added🚨This issue needs some love.This issue needs some love.triage meI really want to be triaged.I really want to be triaged.
on Apr 6, 2020 - added a commit that references this issue
on Nov 9, 2022 - added a commit that references this issue
on Nov 11, 2022 - added 10 commits that reference this issue
on Nov 11, 2022 - added a commit that references this issue
on Nov 16, 2022 - added 2 commits that reference this issue
on Nov 17, 2022 - added a commit that references this issue
on Nov 17, 2022 - added a commit that references this issue
on Nov 18, 2022


nodejs-docs-samples/functions/background/index.js line 27
commit: f7b29b3
introduces 'event.data.myMessage'
IIUC 'event' object does not contain this property and the code does not work:
https://cloud.google.com/functions/docs/writing/background#event_parameter
A different and probably better mechanism is used here:
https://cloud.google.com/functions/docs/tutorials/pubsub#preparing_the_application