I have been working on this page code for a long time and it was all working fine. Then today I tested it again and it’s not working. I have only made a few changes in that time and have blocked out the code that was added to see if that was the issue, but it still isn’t working.
Issue: user should be logged in or registered when submitting form - it appears the function relating to this is no longer working. (I did also change the password variable value but I can’t see why this would be the issue so I didn’t change it back).
My brain is about to implode and I could really use some help because I may be completely missing something now.
This is the page I am working on .
And here is the section of code that the error is coming from:
function registerUser() {
// Get selected programs from program checkboxes.
let checked = [];
if ($w('#coedSpringCheckbox').checked)
checked.push(' Coed Spring 2021 ');
if ($w('#coedSummerCheckbox').checked)
checked.push(' Coed Summer 2021 ');
if ($w('#coedPackageCheckbox').checked)
checked.push(' Coed Spring & Summer 2021 ');
if ($w('#mensSpringCheckbox').checked)
checked.push(' Mens Spring 2021 ');
if ($w('#mensSummerCheckbox').checked)
checked.push(' Mens Summer 2021 ');
if ($w('#mensPackageCheckbox').checked)
checked.push(' Mens Spring & Summer 2021 ');
// Get user details for register or log in.
let firstName = $w("#firstNameInput").value;
let lastName = $w("#lastNameInput").value;
let address = $w("#addressInput") + ", " + $w("#cityInput") + ", " + $w("#postcodeInput");
let initials = firstName.substring(0, 1) + lastName.substring(0, 1);
let phone = $w("#phoneInput").value;
let lastFourDigits = phone.substring(phone.length - 4, phone.length); //substring containing last 4 characters
console.log("Last 4 phone digits:",lastFourDigits);
let dobOptions = {day: "2-digit", month: "2-digit", year: "numeric"};
let dob = $w("#dobDatePicker").value;
let dobString = dob.toISOString("en", dobOptions);
let birthDate = dobString.split(/[- ]/i, 1)[0];
console.log("birthDate =", birthDate);
let email = $w("#emailInput").value;
console.log("Registration email:", email);
let password = initials + lastFourDigits + birthDate;
console.log("Password:", password)
// Find if user has previously registered; if registered: log in, if not yet registered: register and then log in.
wixData.query('soccerMembersDatabase')
.eq('email', email)
.skip(1)
.find()
.then( (results) => {
console.log("SMDatabase query results:", results);
if(results.items.length > 0) {
wixUsers.login(email, password)
.then( () => {
console.log("Existing user is logged in");
} )
.then( () => {
populateRegConfirmation();
})
/*.then( () => {
sendCompanyNotification();
})*/
.catch( (err) => {
console.log("Existing user login error:", err);
} );
} else {
wixUsers.register(email, password, {
contactInfo: {
"firstName": firstName,
"lastName": lastName,
"phones": [phone],
/*"Soccer Reg Info": "www.georgianbaysportsclub.com/soccer-members-database/" + email,*/
"labels": "Mens Spring 2021"
}
} )
.then( (result) => {
let resultStatus = result.status;
console.log("Register new user result status:", resultStatus);
wixUsers.login(email, password);
return resultStatus;
})
.then( () => {
return console.log("New user is logged in");
} )
.then( () => {
populateRegConfirmation();
})
/*.then( () => {
sendCompanyNotification();
})*/
.catch( (err) => {
console.log("New user login error:", err);
} );
}
});
}
Thank you in advance!!
SOLVED:
I’m not 100% sure what the problem was due to in the end as I rewrote the code quite a bit, but after making a lot of changes and it almost working, I blocked out the “labels:” object within contactInfo and it worked! So I’m guessing there may have been an issue with the way I had written it. I suspect it was because I had written an object that was already an array inside of an array haha! I have since added the labels object back without the extra array brackets and the functionality is still working fine, but the contactInfo “labels” is just not being submitted to the contact for some reason.
Here is the working amended code:
function registerOrLoginUser(){
let values = calculateValues();
wixData.query('soccerMembersDatabase')
.eq('email', values.email)
.skip(1)
.find()
.then( (results) => {
return results.items.length > 0 ? login(values.email, values.password) : register(values.email, values.password, values.contactInfo);
});
///
}
function calculateValues(){
// Get selected programs from program checkboxes.
let checked = [];
if ($w('#coedSpringCheckbox').checked)
checked.push(' Coed Spring 2021 ');
if ($w('#coedSummerCheckbox').checked)
checked.push(' Coed Summer 2021 ');
if ($w('#coedPackageCheckbox').checked)
checked.push(' Coed Spring & Summer 2021 ');
if ($w('#mensSpringCheckbox').checked)
checked.push(' Mens Spring 2021 ');
if ($w('#mensSummerCheckbox').checked)
checked.push(' Mens Summer 2021 ');
if ($w('#mensPackageCheckbox').checked)
checked.push(' Mens Spring & Summer 2021 ');
// Get user details for register or log in.
let firstName = $w("#firstNameInput").value;
let lastName = $w("#lastNameInput").value;
let address = $w("#addressInput") + ", " + $w("#cityInput") + ", " + $w("#postcodeInput");
let initials = firstName.substring(0, 1) + lastName.substring(0, 1);
let phone = $w("#phoneInput").value;
let lastFourDigits = phone.substring(phone.length - 4, phone.length); //substring containing last 4 characters
console.log("Last 4 phone digits:",lastFourDigits);
let dobOptions = {day: "2-digit", month: "2-digit", year: "numeric"};
let dob = $w("#dobDatePicker").value;
let dobString = dob.toISOString("en", dobOptions);
let birthDate = dobString.split(/[- ]/i, 1)[0];
console.log("birthDate =", birthDate);
let email = $w("#emailInput").value;
console.log("Registration email:", email);
let password = initials + lastFourDigits + birthDate;
console.log("Password:", password)
let contactInfo = [checked, firstName, lastName, address, phone];
return {email, password, contactInfo};
}
function login(email, password){
wixUsers.login(email, password)
.then( () => {
console.log("Existing user is logged in");
} )
.then( () => {
populateRegConfirmation();
})
//.then( () => {
//sendCompanyNotification();
//})
.catch( (err) => {
console.log("Existing user login error:", err);
} );
}
function register(email, password, contactInfo) {
wixUsers.register(email, password, {
contactInfo: {
"firstName": contactInfo[1],
"lastName": contactInfo[2],
"phones": [contactInfo[4]],
//"Soccer Reg Info": "www.georgianbaysportsclub.com/soccer-members-database/" + email,
"labels": contactInfo[0],
}
} )
.then( (result) => {
let resultStatus = result.status;
//console.log("Register new user result status:", resultStatus);
wixUsers.login(email, password);
//return resultStatus;
})
.then( () => {
//return
console.log("New user is logged in");
} )
.then( () => {
populateRegConfirmation();
})
//.then( () => {
//sendCompanyNotification();
//})
.catch( (err) => {
console.log("New user login error:", err);
} );
}