Access form data in event-triggered function

I have a function that is triggered by a form submission with submission-created. I want to access the form fields submitted in the function, but cannot see how to access them.

I submit the form using the following:

    fetch("/", {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body: encode({ "form-name": "telephone", phonenumber: phoneNumber }),
    })

I then try and access the phonenumber field like so:

const params = querystring.parse(event.body)
const telephone = params.phonenumber || “Not Set”

The above returns ‘Not Set’ and I cannot see any other way to access the data. Any points would be useful.

Hiya David!

I wonder if you could try a pattern like the one in @cfjedimaster’s blog post here:

https://www.raymondcamden.com/2019/01/15/customized-form-handling-on-netlify-with-serverless-functions

He has a pretty good walkthrough and seems to parse the event body as JSON first which may help you out.

1 Like

Yes that did it.

Get the function data with
JSON.parse(event.body).payload

And the form values are in the data block of the JSON

Thanks!

1 Like