Function working locally, not when deployed

Hello,

I have a function submission-created.js to delete Netlify Forms submissions as they come in, firing on the Netlify submission-created event.

Locally, using netlify dev and submitting a Form from localhost:8888, I can see the submissions are all deleted from the Netlify dashboard. This also works by manually calling it with netlify functions:invoke submission-created

Deployed and via a deploy preview URL, entries are not deleted when I submit a new form.

This is the function and repo https://github.com/dazulu/cello-teacher/blob/master/lambda-src/submission-created.js
Preview URL of deploy: https://5f59e25719c7640007ff0e0f--cello-lessons.netlify.app/

Update
I’ve since switched to using netlify-lambda, hoping that might resolve the issue but same results as above. Working locally but not deployed.

Thanks!

Been continuing trying to figure this out, reworking with netlify-lambda etc. but still no luck in getting it to work when deployed. Latest deploy preview: https://5f5bbebd467d4a00079417aa--cello-lessons.netlify.app/

I really have no idea if it’s firing or not when deployed. I can only assume it isn’t because the form submissions are not deleted. It has never appeared in the Functions UI and the Netlify Support twitter account told me this is normal for lambda functions that are Netlify event hooks like mine (https://twitter.com/NetlifySupport/status/1303766897536581632) Really hard to debug this :smiley:

The env vars are correctly set. I have one set custom and the other, SITE_ID, is a default

Deploy log slice so I know that it is being picked up during the build.

8:16:32 PM: │ 2. onPostBuild command from @netlify/plugin-functions-core │
8:16:32 PM:
8:16:32 PM: ​
8:16:32 PM: Packaging Functions from lambda directory:
8:16:32 PM: - submission-created.js
8:16:45 PM: ​
8:16:45 PM: (@netlify/plugin-functions-core onPostBuild completed in 13s)

Update
Created a duplicate of the function with a non-event based name. Deployed it. Visited it in the browser e.g. /.netlify/functions/my-test-function. Seems to work.

I’ve rolled everything back now so the original link won’t work (can’t edit the original post anymore?) Here is the link where everything was still included https://github.com/dazulu/cello-teacher/tree/596cda02407e0e9e1d3343fcacd50fcb39b579a1

I’m just going to go with this is likely not possible due to the timings of the function and form submission? I will try to find another way.

Hey @dazulu,
I dug into this because I was like “that should totally work!” and in fact it does! I think there is something weird going on with with async/await and .forEach, maybe: https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404? I’m not sure to be honest, but using an old school for loop instead worked for me:

# submission-created.js

const NetlifyAPI = require('netlify')

exports.handler = async function (event, context) {
  const client = new NetlifyAPI(process.env.NETLIFY_PAT)

  const submissions = await client.listSiteSubmissions({
      site_id: "628de7ef-04fb-4530-a7da-b795c0e5a457" <--- no worries, this is not sensitive
    })
    .catch((e) => console.log('Error getting submissions', e))

  if (submissions.length) {
    for (i = 0; i < submissions.length; i++) {
      console.log('starting to delete submission: ', submissions[i].id)
      await client.deleteSubmission({ submission_id: submissions[i].id })
      console.log('deleted submission: ', submissions[i].id)
    }
  } else {
    console.log('NOTHING TO DELETE')
  }
}

Thanks @jen Good article! A lesson in imperative vs declarative, perhaps :sweat_smile:

I was just about to reply with the post below (which I’m going to leave in case it helps anyone out), and it occurred to me that I never tested it deployed to production. I had only tested it working off the build preview URLs because I didn’t want the site live yet.

Long story short, I temporarily deployed it live and the function fires. It would seem event-based functions are not set up to fire from Preview Deploys :confounded:

I’m sure I didn’t miss this footnote in the docs somewhere. Though I could be wrong. Please link me if anyone sees it. Otherwise this should probably be clarified there or, if it’s a bug, thrown in the backlog :sweat_smile:

Thanks again @jen - legend - I had given up on this and wouldn’t have tried again had you not replied!


Post below for historical purposes
The submission-created event function (only one I tested) only fires on a live site. It is not working on preview URLs.

I’ve adjusted the function but still something is not right when deployed :confused:

Test 1/2

  1. Tested the refactored function locally with netlify dev and netlify functions:invoke submission-created:
    • Forms visible on the Netlify Admin dashboard are deleted :white_check_mark:
  2. Submitting a form deployed on Netlify:
    • Forms visible on the Netlify Admin dashboard are not deleted :x:

Build Log Extract

Just so we see the netlify dependency gets installed and the function is picked up.

logs

Test 2/2

  1. I duplicated the function (duplicate.js) so that I can call it manually when deployed (read that you can’t call deployed event-based functions manually - you get 403’d)
  2. Called it in the browser - Get our console log from the function and forms visible on the Netlify Admin dashboard are deleted :white_check_mark:

I am at a loss :sweat_smile:

submission-created.js

const NetlifyAPI = require('netlify')

exports.handler = async function (event, context) {
  const client = new NetlifyAPI(process.env.NETLIFY_API_ACCESS_TOKEN)

  const submissions = await client
    .listSiteSubmissions({
      site_id: process.env.SITE_ID
    })
    .catch((e) => console.log('Error getting submissions', e))

  if (submissions.length) {
    for (i = 0; i < submissions.length; i++) {
      const submissionID = submissions[i].id
      console.log('starting to delete submission: ', submissionID)
      await client.deleteSubmission({ submission_id: submissionID })
      console.log('deleted submission: ', submissionID)
    }
    return {
      statusCode: 200,
      body: 'Submissions deleted',
    }
  } else {
    console.log('No submissions to delete')
    return {
      statusCode: 200,
      body: 'No submissions to delete',
    }
  }
}

Got a blog post out of this whole thing :slight_smile: Here’s how to create a Netlify Function to automate the deletion of form submissions when a user submits a form (you obviously want to be using the Netlify notifications system to forward them to yourself first) https://frontend.irish/netlify-delete-form-submissions-gdpr

1 Like

Ayyyy, what a great post! Many thanks for the work you put into that :trophy: If you wanted to start a topic explaining why you pursued this and sharing the article, please feel free- people may find it a bit more easily that way than by following this meandering thread :slight_smile:

1 Like