Hi,
I have found a lot of articles looking to integrate Wix forms with MailChimp using wix-fetch, particularly @Toby's post. However, although I followed the other resources exactly, it is not working for me. Here is my code.
Backend:
import {fetch} from 'wix-fetch';
import {getSecret} from 'wix-secrets-backend'
const API_KEY_64 = getSecret('MailChimp');
const LIST_ID = "d4b50956ba";
export function sendToMailChimp(data) {
fetch("https://us6.api.mailchimp.com/3.0/lists/" + LIST_ID + "/members", {
method: 'POST',
headers: {
'Authorization' : 'apikey ' + API_KEY_64,
'Content-Type' : 'application/json'
},
body: data
})
.catch(err => console.log('ugh'));
}
// Execute the sample function above by copying the following into your page code
Frontend:
import {sendToMailChimp} from 'backend/mailChimp.jsw'
export function getSubscribers1_wixFormSubmitted(fields) {
console.log('hook 2.0')
let lastName
let email = fields.fields[2].fieldValue
let firstName = fields.fields[0].fieldValue
if(fields.fields[1].fieldValue !== '') {lastName = fields[1].fieldValue}
else {lastName = ' '}
if(typeof email === 'string' && typeof firstName === 'string') {
var data = {
"email_address":email,
"merge_fields": {
"FNAME":firstName
},
"status":"subscribed"
};
// Stringify into JSON format
let stringData = JSON.stringify(data);
// Process request
console.log(data)
console.log(stringData)
sendToMailChimp(stringData);
}
return data;
}
Everything in the front end code is working great. My console.log results get me:
{"email_address":"a@a.com","merge_fields":{"FNAME":"A"},"status":"subscribed"}
Which is exactly what I am looking for. However, it is not translating to MailChimp.
Any help greatly appreciated! Thanks.