netlify site name = Summertime | Make an enquiry for our luxury villa in Goa
error message = Jun 24, 12:57:15 PM: 7af5f746 ERROR Invoke Error {“errorType”:“SyntaxError”,“errorMessage”:“Unexpected end of JSON input”,“stack”:[“SyntaxError: Unexpected end of JSON input”," at JSON.parse ()“,” at Runtime.exports.handler (/var/task/functions/contact/contact.js:11:19)“,” at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25)"]}
The form “contact-us” works well and we receive the data submitted. In the functions folder we have a file called “contacts.js” which is meant to sent a message with the same data to the person who entered the form, in this case me. However I get an error about the JSON.parse line.
exports.handler = async (event, context, callback) => {
/* your server-side functionality */
const data = JSON.parse(event.body)
I have no idea how or where to debug this. It used to work fine so I am at a total loss as other messages have not taught me much.
This is more or less the complete code. Should mailgun-js be replaced with mailgun.js or nodemailer?
require('dotenv').config()
const { MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_URL, FROM_EMAIL_ADDRESS, CONTACT_TO_EMAIL_ADDRESS } = process.env
const mailgun = require('mailgun-js')({ apiKey: MAILGUN_API_KEY, domain: MAILGUN_DOMAIN, host: "api.eu.mailgun.net" })
exports.handler = async (event, context, callback) => {
/* your server-side functionality */
const data = JSON.parse(event.body)
var text = `Dear ${data.name}, \n\n`
text += `\n\nYour message: \n\n`
text += `${data.message} \n`
text += `Country of residence: ${data.country} \n`
const style = "<style> </style>"
var body = `<html>${style}<div>`
body += `<p>Dear ${data.name},</p>`
body += `<h4>Your message:</h4><p>${data.message}</p>`
body += `<p>Country of residence: ${data.country}</p>`
body += `</div></html>`
let response
try {
response = await mailgun.messages().send({
to: data.email,
bcc: CONTACT_TO_EMAIL_ADDRESS,
from: FROM_EMAIL_ADDRESS,
subject: `Enquiry for Summertime from ${data.arrdate}`,
text: text,
html: body
})
}
catch (error) {
console.log('Error: ', error)
return {
statusCode: error.statusCode || 500,
headers: { "Access-Control-Allow-Origin": "*" },
body: JSON.stringify({ error: error.message })
}
}
return {
statusCode: 200,
headers: { "Access-Control-Allow-Origin": "*" },
body: JSON.stringify({ result: 200 })
}
}