DEV Community

loading...

Discussion on: Hitting a Cloud Function when you submit a Google Form

Collapse
di profile image
Dustin Ingram Author

Hi Jouwert, happy to help. What's the error?

Collapse
jouwert profile image
Jouwert van Geene • Edited

I am trying to upload screenshot but I fail ...
This is what Google Cloud gives me back:
Implementatiefout:
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/index.js:1
(function (exports, require, module, __filename, __dirname) { def form_trigger(request):
^^^^^^^^^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at getUserFunction (/worker/worker.js:439:24)

Thread Thread
jouwert profile image
Jouwert van Geene

The trigger script in my google form is

// Replace with the URL to your deployed Cloud Function
var url = "us-central1-inspired-rush-271907.c..."

// This function will be called when the form is submitted
function onSubmit(event) {

// The event is a FormResponse object:
// developers.google.com/apps-script/...
var formResponse = event.response;

// Gets all ItemResponses contained in the form response
// developers.google.com/apps-script/...
var itemResponses = formResponse.getItemResponses();

// Gets the actual response strings from the array of ItemResponses
var responses = itemResponses.map(function getResponse(e) { return e.getResponse(); });

// Post the payload as JSON to our Cloud Function

UrlFetchApp.fetch(
url,
{
"method": "post",
"payload": JSON.stringify({
"responses": responses
})
});
};

Thread Thread
jouwert profile image
Jouwert van Geene

But maybe the Cloud Function is wrong. I was not so sure as to how to implement this script from your instruction:

def form_trigger(request):
payload = request.get_json(silent=True)
print(f"Payload was: {payload}")

return "OK"