Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Code in Cloud Function

Hiiii, could somebody tell me what's wrong with the following code that I keep getting error: TypeError: hello_pubsub() takes 1 positional argument but 2 were given

def
hello_pubsub(data):
    data = 0
    data = 1 + 1
    print(data)
 
It just doesn't make sense to me. Thank you in advance!
1 2 689
2 REPLIES 2

Hi @Dionzzz,

Welcome to Google Cloud Community!

This error is caused by the main function in the Cloud Function code doesn't accept two arguments. To fix this, you need to modify the main function to accept two arguments:

def hello_pubsub(message, context)

If the result is the following error, remove any main calls without parameters or update the calls with two parameters, as appropriate.

Error message: hello_pubsub() missing 2 required positional arguments: 'data' and 'context'

Please refer to this documentation on TypeError shown while using a Cloud Function triggered by a Pub/Sub API.

You may also check the following Stack Overflow link as reference:

Hope this helps.

Do we just call the functions we wrote in the main function like "hello_pubsub" in my case?

For example:

def sum():
    data = 0
    data = 1 + 1
    print(data)

@functions_framework.cloud_event
def hello_pubsub(cloud_event):
       sum()
       # Print out the data from Pub/Sub, to prove that it worked
       print(base64.b64decode(cloud_event.data["message"]["data"]))