Hi everyone,
i’m working on a website with nuxt in Server Side Rendering, deployed on Netlify.
I’m adding a new feature for subscribing users to a Mailchimp list, and in order to make everything custom, i decided to use their APIs.
I need to do a POST request to their endpoint, using the secret keys. What I did kinda works, but i can’t understand how to deal with the response i receive from the function, in order to manipulate it then in the component; here’s my code:
In the component:
let subscriber = await this.$axios.$post(`https://deploy-preview-1--fasalwebsite.netlify.app/.netlify/functions/subscribe`,
{
"email_address": this.email,
"status": "subscribed",
"merge_fields": {
"FNAME": this.name,
"LNAME": this.surname
},
"tags":[
this.type == 'private' ? "privato" : "azienda"
]
},
{
headers: {"Accept": "application/json", "Content-Type": "application/json"},
}
)
In subscribe.js (the serverless function):
const axios = require('axios')
exports.handler = async (event) => {
try {
const res = await axios({
method: 'post',
url: `https://us17.api.mailchimp.com/3.0/lists/52e8c44f16/members`, //change region (us19) based on last values of ListId.
data: event.body,
auth: {
username: 'apikey', // any value will work
password: process.env.KEY_CHIMP
}
}
)
console.log(res.data)
return {
statusCode: 200,
body: res.data
}
}
catch (err) {
return {
statusCode: 400,
body: err.message
}
}
}
process.env.KEY_CHIMP is registered in Netlify Env Variables of course.
New users are successfully added, but i don’t know how to handle the response I get from the function.
Anybody can help me understand?