Netlify functions: Unexpected token in JSON & not deploying on preview

My first post on here so forgive if I’ve missed anything salient out.

I have a Gatsby site bootstrapped from a started for a friend studying for a PhD: GitHub - SeanAlexanderHarris/example-company-website-gatsby-sanity-combo: This is an example company website using Gatsby and Sanity in combination. /

I am trying to add a contact form that will use SendGrid to send an email to him.

I have a Netlify function in a functions folder called ‘contactBealth’.
I also have a simple hello world test function in another folder called ‘hello’.
This work has been done on a branch & as such is triggering a preview deploy.

There are a few issues I think are related:

  • although the build logs for the preview deploy acknowledge the functions, none are actually deployed (they do not appear in the Netlify functions dashboard ui)
  • both functions are invokable with the Netlify cli netlify functions:invoke contactBealth & netlify functions:invoke hello & return a response.
  • both functions return a response when running locally and appear to build as I can console log throughout the function
  • when visiting the function url (http://localhost:8888/.netlify/functions/contactBealth), the hello function returns a response but the contactBealth function result in the following locally:
SyntaxError: Unexpected token u in JSON at position 0
  JSON.parse (<anonymous>)
  Object.exports.handler (/Users/seanharris/Code/bealthcorp/web/functions/contactBealth/contactBealth.js:13:21)
  Object._executeSync (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:286:47)
  Object.execute (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:83:22)
  executeSynchronousFunction (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/src/utils/serve-functions.js:102:15)
  handler (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/src/utils/serve-functions.js:248:12)
  Layer.handle [as handle_request] (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/express/lib/router/layer.js:95:5)
  next (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:137:13)
  next (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)
  next (/Users/seanharris/.nvm/versions/node/v14.15.0/lib/node_modules/netlify-cli/node_modules/express/lib/router/route.js:131:14)

My toml file is:

  base = "web/"
  publish = "public"
  functions = "functions"
  command = "npm run build"

Hey there,
I think there maybe isn’t an event.body where you’re expecting one? So when you JSON.parse, it breaks the whole function. I am seeing that the functions are deployed (our UI for checking function logs on branch deploys/deploy previews is admittedly not great, but hopefully this helps!):

And going forward, you can get to that view by clicking the “Functions” link in the deploy log:

Thank Jen. You know what… I realised that the functions from the preview deploy only appeared when I started entering text input into what seemed initially like just a drop down…:man_facepalming:

From there I can see the logs I’d expect and can corroborate it is behaving from the SendGrid dashboard as well.

Thank :slight_smile:

Awesome, glad to hear it!