Skip to content

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

Description

@DazWilkin

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

Activity

  1. jasonpolites commented on Nov 17, 2016

    @jasonpolites
    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

  2. jmdobry commented on Nov 17, 2016

    @jmdobry
    Member

    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

  3. jmdobry commented on Nov 17, 2016

    @jmdobry
    Member

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

  4. DazWilkin commented on Nov 17, 2016

    @DazWilkin
    Author

    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)
    
  5. jmdobry commented on Nov 17, 2016

    @jmdobry
    Member

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

  6. jmdobry commented on Aug 21, 2017

    @jmdobry
    Member

    I removed this sample from the docs.

  7. added a commit that references this issue on Nov 17, 2022
  8. added a commit that references this issue on Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🚨This issue needs some love.triage meI really want to be triaged.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions