Question:
I don’t know what’s the problem with my code anymore
my last error was -
Product:
Wix studio, Velo.
What are you trying to achieve:
I have a custom newsletter form in my site, When clicking submit the code will check if the user is in the Contact list of the site, check if he’s subscribed to the new letter, check the if the mail formet is OK and Check if the users Consents via the checkbox.
What have you already tried:
I tried using ChatGPT, Wix Ai and some YouTube videos. but I haven’t managed to solve this.
This is my code -
Frontend
import { getOrCreateContact } from 'backend/contact';
$w.onReady(function () {
$w('#submit').onClick(async () => {
const email = $w('#mail').value;
const isChecked = $w('#Verify').checked;
// Validate email format
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
console.error('Invalid email format');
return;
}
// Check if checkbox is checked
if (!isChecked) {
console.error('Checkbox must be checked');
return;
}
console.log('Attempting to create or update contact with email:', email);
try {
const contact = await getOrCreateContact(email);
console.log('Contact:', contact);
} catch (error) {
console.error('Error checking or creating contact:', error);
}
});
});
Backend
import { contacts } from 'wix-crm-backend';
import wixData from 'wix-data';
export async function getOrCreateContact(email) {
try {
console.log('Checking if contact exists for email:', email);
// Check if the contact already exists
if (existingContact.items.length > 0) {
console.log('Contact already exists:', existingContact.items[0]);
return existingContact.items[0];
} else {
console.log('Contact not found, creating new contact');
// Create new contact
const contactInfo = {
emails: [{ email: email }],
subscriptionConsent: {
consent: true,
subscriptionType: 'MARKETING'
}
};
const newContact = await contacts.createContact(contactInfo);
console.log('Created new contact:', newContact);
// Subscribe the new contact to the newsletter
await subscribeToNewsletter(email);
return newContact;
}
} catch (error) {
console.error('Error in getOrCreateContact:', error);
throw new Error('Failed to create or retrieve contact');
}
}
async function subscribeToNewsletter(email) {
try {
console.log('Checking if email is subscribed to the newsletter:', email);
// Check if the email is already in the newsletter list
const newsletterList = await wixData.query('NewsletterSubscriptions')
.eq('email', email)
.find();
if (newsletterList.items.length === 0) {
// If the email is not subscribed, add it to the newsletter list
await wixData.insert('NewsletterSubscriptions', {
email: email,
subscriptionDate: new Date(),
status: 'subscribed'
});
console.log('Successfully subscribed to the newsletter');
} else {
console.log('Email is already subscribed to the newsletter');
}
} catch (error) {
console.error('Error in getOrCreateContact:', error);
console.error('Error message:', error.message);
console.error('Error stack:', error.stack);
throw new Error('Failed to create or retrieve contact');
}}