Hi Nick,
I’ve followed your instructions to a “T”. I’ve read and re-read my code at least 100 times. Can you help me figure out what’s wrong? I’m getting the expected success message through the console but the emails never show up in my list. I’ve checked my list ID, I’ve checked my API key. Any more thoughts? I’ve replaced my list ID and code with “[MASKED]”.
BACKEND: maillChimp.jsw
import {fetch} from ‘wix-fetch’;
export function addSubscriber(email) {
fetch("https://us10.api.mailchimp.com/3.0/lists/[MASKED]/members/", {
body: '{\"email_address\": \"' + email + '\", \"status\": \"subscribed\"}',
headers: {
Authorization: "apikey [MASKED]",
“Content-Type”: “application/json”
},
method: “POST”
})
}
PAGE
import wixUsers from ‘wix-users’;
import wixWindow from ‘wix-window’;
import wixLocation from ‘wix-location’;
import {addSubscriber} from ‘backend/mailChimp’;
// Allergy Switch Toggle
function toggleFold(index) {
let $fold = $w(‘#fold’ + index);
// toggle the fold at the index
if ($fold.collapsed) {
$fold.expand();
}
else {
$fold.collapse();
}
}
export function allergySwitch_click(event) {
toggleFold(1);
}
$w.onReady( function () {
// Sign-Up Form LIVE Personalizations
$w(‘#ThankYouText’).text = “”;
$w(‘#ReferredByLastName’).onBlur(() => {
let nbsp = String.fromCharCode(160) //returns non-breaking space as HTML text
if ($w(‘#ReferredByFirstName’).value !== “”) {
$w(‘#ThankYouText’).text = nbsp+"Thank you! We’ll send some extra
" + $w(‘#ReferredByFirstName’).value + “'s way.”+nbsp;
}
});
$w('#firstName').onBlur(() => {
if ($w(‘#firstName’).value !== “”) {
$w(‘#greeting’).text = "Nice to meet you, " + $w(‘#firstName’).value + “!”;
}
});
// Form Validation
$w(‘#validationMessages’).collapse();
const validatePassword = (otherPasswordElementID) => (value, reject) => {
let otherPasswordElement = $w(otherPasswordElementID);
if (value === otherPasswordElement.value) {
otherPasswordElement.validity.valid = true ;
otherPasswordElement.resetValidityIndication();
return ;
}
console.log("Passwords do not match");
otherPasswordElement.validity.valid = **false** ;
otherPasswordElement.updateValidityIndication();
reject("Passwords do not match");
};
$w("#passwordVerify").onCustomValidation(validatePassword("#password"));
$w("#password").onCustomValidation(validatePassword("#passwordVerify"));
$w('#submit').onClick(() => {
if (!($w(‘#firstName’).valid && $w(‘#lastName’).valid && $w(‘#email’).valid && $w(‘#Hugs’).valid &&
$w(‘#phoneNumber’).valid && $w(‘#ReferredByFirstName’).valid && $w(‘#ReferredByLastName’).valid && $w(‘#password’).valid)) {
let validationMessage = ‘’;
if (!$w(‘#ReferredByFirstName’).valid || !$w(‘#ReferredByLastName’).valid) {
validationMessage += ‘Please enter the full name of the person who referred you.\n’;
}
if (!$w(‘#firstName’).valid || !$w(‘#lastName’).valid) {
validationMessage += ‘Please enter your full name. \n’;
}
if (!$w(‘#phoneNumber’).valid) {
validationMessage += ‘Please enter a valid phone number. \n’;
}
if (!$w(‘#email’).valid) {
validationMessage += ‘Please enter a valid email address. \n’;
}
if (!$w(‘#Hugs’).valid) {
validationMessage += ‘Please select your hug preference. \n’;
}
if (!$w(‘#password’).value)
validationMessage += ‘Please choose a password. \n’;
if ($w(‘#password’).value.length < 4 && $w(‘#password’).value.length > 0)
validationMessage += ‘Please choose a password greater than 4 characters. \n’;
if ($w(‘#password’).value.length > 24 )
validationMessage += ‘Please choose a password less than 24 characters. \n’;
if ($w(‘#password’).value !== $w(“#passwordVerify”).value) {
validationMessage += ‘Passwords do not match. \n’;
}
$w('#validationMessages').text = validationMessage;
$w('#validationMessages').expand();
} **else** {
$w('#validationMessages').collapse();
let emails = ;
let phones = ;
let allergies = ;
// Allergy Labels
if ($w(‘#eggs’).checked)
allergies.push(‘Egg Allergy’);
if ($w(‘#soy’).checked)
allergies.push(‘Soy Allergy’);
if ($w(‘#wheat’).checked)
allergies.push(‘Wheat Allergy’);
if ($w(‘#milk’).checked)
allergies.push(‘Milk Allergy’);
if ($w(‘#peanuts’).checked)
allergies.push(‘Peanut Allergy’);
if ($w(‘#shellfish’).checked)
allergies.push(‘Shellfish Allergy’);
if ($w(‘#treeNuts’).checked)
allergies.push(‘Tree Nut Allergy’);
if ($w(‘#fish’).checked)
allergies.push(‘Fish Allergy’);
if ($w(‘#fold1’).collapsed) {
allergies = ;
$w(‘#eggs’).checked = false ;
$w(‘#soy’).checked = false ;
$w(‘#wheat’).checked = false ;
$w(‘#milk’).checked = false ;
$w(‘#peanuts’).checked = false ;
$w(‘#shellfish’).checked = false ;
$w(‘#treeNuts’).checked = false ;
$w(‘#fish’).checked = false ;
}
var value1 = $w(‘#ReferredByFirstName’).value;
var value2 = $w(‘#ReferredByLastName’).value;
var ReferredBy = value1 + " " + value2;
emails.push($w('#email').value);
phones.push($w('#phoneNumber').value);
// register as member using form data
wixUsers.register($w(‘#email’).value, $w(‘#password’).value, {
“contactInfo”: {
“firstName”: $w(‘#firstName’).value,
“lastName”: $w(‘#lastName’).value,
“phones”: phones,
“emails”: emails,
“ReferredBy”: ReferredBy,
“Hugs”: $w(‘#Hugs’).value,
“labels”: allergies
}
});
$w('#submit').collapse();
$w('#validationMessages').expand();
$w('#validationMessages').text = 'Thank you, ' + $w('#firstName').value + '! \n Your request has been recieved.\n Please check your email for confirmation and next steps.\n ❤️ TCB';
}
});
});
// MailChimp Integration
export function submit_click(event, $w) {
//Add your code for this event here:
let email = $w(“#email”).value;
addSubscriber(email).then(result => {
console.log(‘Email subscribed to Newsletter list.’)
})
. catch (error => {
console.log("Subscribe Now error: " + error);
});
}