Info:
site name: https://jordanjackson.netlify.app/
lambda func. name: sendNewsletterV2
Problem
Hi netlify community! I ran into a problem regards netlify functions. I uploaded a function with a request to mailchimp. The request works, and I can see on netlify, that function is successfully executed and sent to mailchimp with status code 200. Mailchimp also registers a new subscriber. The problem is that I am getting back 502 on my frontend (Nuxt.js). In my Nuxt.js code, my success promise is executed. So when I am printing to the console it shows success message, but with status code 502.
Thank you so much for your time!
Code
require('dotenv').config()
const Mailchimp = require('mailchimp-api-v3')
const mailChimpAPI = process.env.mailChimpAPI;
const mailChimpListID = process.env.mailChimpListID;
const mailchimp = new Mailchimp(mailChimpAPI)
exports.handler = function(event, context, callback) {
const data = JSON.parse(event.body)
if (data.email_address) {
mailchimp
.post(
'/lists/' + mailChimpListID + '/members?skip_merge_validation=true',
{
email_address: data.email_address,
status: 'subscribed',
merge_fields: {
FNAME: data.first_name,
LNAME: data.last_name,
COUNTRY: data.country
}
}
)
.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 subscriber already exists!'
}
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: 'Missing fields.'
})
}
callback(null, response)
}
}