I am not understanding how to pass form submission data to custom (serverless?) functions.
As I understand it, I can set outgoing Webhooks upon form submissions and can send a POST request to a custom function, which includes the payload from the form submission, by specifying the URL of the Webhook as [myNetlifyDomain]./netlify/functions/testFunc
.
In my testFunc
function, I have:
exports.handler = async (event, context, callback) => {
console.log(event, context, callback);
const body = JSON.parse(event.body).payload;
console.log(body);
return callback(null, {
statusCode: 200,
body: body
});
}
I am trying to log the context of the form submission so I can then create a call to a 3rd-party API in order to add users to my mailing list.
When I fill out and submit a form, I go to see the output of testFunc
and I get this:
{"errorMessage":"Unexpected end of JSON input","errorType":"SyntaxError","stackTrace":["JSON.parse (<anonymous>)","n.handler (/var/task/testFunc.js:1:1104)"]}
This appears both on the front-end from the URL: https://practical-raman-142310.netlify.com/.netlify/functions/testFunc
and from the Netlify back-end functions log.
My forms typically include two fields, one for name and one for a comment or email.
I was originally trying to process the mailing list user creation from the submission-created.js
function but couldn’t successfully troubleshoot submission errors as the function could not be accessed directly from the URL (permission denied error) and I couldn’t see enough of the error in the Netlify functions log (the log would be cut off inexplicably).
Perhaps I am going about this the wrong way but I contacted someone from Netlify a couple months ago and they told me about this method of passing form submitted data to a custom function.
Any help troubleshooting this would be greatly appreciated.
Thank you,