Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Functions background sample appears incorrect: entity.data.myMessage #260

Closed
DazWilkin opened this issue Nov 17, 2016 · 6 comments
Closed
Labels
🚨 This issue needs some love. triage me I really want to be triaged.

Comments

@DazWilkin
Copy link

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

@jasonpolites
Copy link
Contributor

Which property do you mean? myMessage or data?

event.data exists
event.data.myMessage is just an example. Perhaps we need to be clear about how myMessage is populated in that example

@jmdobry
Copy link
Member

jmdobry commented Nov 17, 2016

I 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:

screen shot 2016-11-17 at 10 32 49 am

The success case:

$ gcloud alpha functions call helloWorld --data '{"data":{"myMessage":"Hello, world!"}}'

executionId: xhsrgavertuo

screen shot 2016-11-17 at 10 29 58 am

@jmdobry
Copy link
Member

jmdobry commented Nov 17, 2016

I agree though, it's not clear what the use case is for that particular sample.

@DazWilkin
Copy link
Author

DazWilkin commented Nov 17, 2016

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 event

In 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-PROJECT

I 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-BUCKET

I 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)

@jmdobry
Copy link
Member

jmdobry commented Nov 17, 2016

You are right, this particular sample only works if you call it with gcloud alpha functions call. 😕

@jmdobry
Copy link
Member

jmdobry commented Aug 21, 2017

I removed this sample from the docs.

@jmdobry jmdobry closed this as completed Aug 21, 2017
@yoshi-automation yoshi-automation added 🚨 This issue needs some love. triage me I really want to be triaged. labels Apr 6, 2020
pattishin pushed a commit that referenced this issue Nov 9, 2022
#260)

* build(node): add feat in node post-processor to add client library version number in snippet metadata

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Source-Link: googleapis/synthtool@d337b88
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:d106724ad2a96daa1b8d88de101ba50bdb30b8df62ffa0aa2b451d93b4556641
ace-n pushed a commit that referenced this issue Nov 11, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 11, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 14, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
ace-n pushed a commit that referenced this issue Nov 14, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
ace-n pushed a commit that referenced this issue Nov 14, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 14, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
ace-n pushed a commit that referenced this issue Nov 15, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
ace-n pushed a commit that referenced this issue Nov 15, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 15, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 17, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ace-n pushed a commit that referenced this issue Nov 17, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
ahrarmonsur pushed a commit that referenced this issue Nov 17, 2022
* updated CHANGELOG.md [ci skip]

* updated package.json [ci skip]

* updated samples/package.json [ci skip]
kweinmeister pushed a commit that referenced this issue Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 This issue needs some love. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

4 participants