I have a simple form that takes data and subscribes someone to a Mailchimp list. It was working fine until someone reported errors overnight (built the form to handle errors and display them to the user).
Turns out the lambdas are still running fine, but started responding with 502 Bad Gateway overnight - seemlingly without any changes that I made.
I’ve tried redeploying a couple of times, but it’s not fixing the problem. I’m constructing responses with 200 status codes in the lambda and those are not making it through, however, the response messages are.
Able to reproduce via postman.
Thinking this is a Netlify issue, but maybe it’s something I’m doing. Pasted my function code below:
Function code below:
require('dotenv').config()
const Mailchimp = require('mailchimp-api-v3')
const mailChimpAPI = process.env.MAILCHIMP_API_KEY
const mailChimpListID = process.env.MAILCHIMP_LIST_ID
const mailchimp = new Mailchimp(mailChimpAPI)
exports.handler = function(event, context, callback) {
const data = JSON.parse(event.body)
console.log(data)
if (data.email && data.selectedState) {
mailchimp
.post(
'/lists/' + mailChimpListID + '/members?skip_merge_validation=true',
{
email_address: data.email,
merge_fields: {
STATE: data.selectedState
},
status: 'subscribed'
}
)
.then(function(results) {
console.log(results)
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
},
body: JSON.stringify({ status: 'Success!' })
}
callback(null, response)
})
.catch(function(err) {
console.log(err)
if (err.title == 'Member Exists') {
err.detail = 'This email address is already subscribed.'
}
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
},
body: JSON.stringify({
status: 'Error',
title: err.title,
detail: err.detail
})
}
callback(null, response)
})
} else {
const response = {
statusCode: 400,
headers: {
'Access-Control -Allow-Origin': '*',
'Access-Control-Allow-Credentials': true
},
body: JSON.stringify({
status: 'Error',
message: 'One or more fields are missing'
})
}
callback(null, response)
}
}