Just to help you understand Andreas’ comment regarding a Promise. There are a couple of things that you should get used to doing.
- Get used to using the API reference for Wix Code…
Velo API Reference - Wix.com
So as an example the API reference for wix-users includes register(). The syntax for the register() function tells us that it returns something, that Andreas referenced above, called a Promise. The Promise returns something called a RegistrationResult or it will throw an exception to return an error.
You can think of a Promise as a way to track how a function call is progression. These are important because some functions we call need to talk to another computer or program which can take time. When the function call completes it has to keep its promise to you and either give you the result you want OR tell you about an error.
To get the result you add a
then() function, which handles the promise, as shown in the example above which gives you an argument called
result to work with. The
result variable is a RegistrationResult object (see below).
If an error occurs then you use a
.catch() function. This catches exceptions and, like the
.then() call, gives an argument which contains the error message.
The RegistrationResult:
2. Field validations are fairly easy to manage. What you need to do is two things.
a. For each element you want to validate set up the field validation in the element settings:
b. Then in your code you can check the validity of the inputElement using the valid property:
So using the register function in your example you can see if the phone number is valid like this
if ($w('#phone').valid) {
// Do something with a valid phone number
}
Of course you can check multiple input values for validity like this:
if ($w('#phone').valid && $w('#email').valid && $w('#name').valid) {
// Do something with a valid phone number and email and name
}
- If there is a registration error you can handle this in the .catch() function. What you can do is use a lightbox for messages. This can be used for errors or success messages. A lightbox pops up over the screen with a message. It has a control to allow you to remove it. Assuming you have a lightbox called ‘Show Message’, you can show a success or fail message pop up like this:
import wixWindow from 'wix-window';
...
$w.onReady(function () {
$w('#submit').onClick(submitRegister);
});
function submitRegister() {
if (valuesValid()) {
let contactToAdd = {
"firstName": $w('#name').value,
"username": $w('#username').value,
"Phone": `${$w('#countrycode').value} ${$w('#phone').value}`,
"reference": $w('#reference').value,
"DOB": `${$w('#day').value}/${$w('#month').value}/${$w('#year').value}`,
"country": $w('#country').value,
"city":$w('#city').value
};
wixUsers.register(
$w('#email').value,
$w('#password').value,
{"contactInfo": contactToAdd})
.then((result) => {
// popup Success Message
popUpMessage("SUCCESS: "+$w('#email').value+" Registered");
})
.catch((error) => {
// popup Error
popUpMessage(error);
});
} else {
// Missing or invalid property
popUpMessage("ERROR: Missing or invalid property");
}
}
function valuesValid() {
// Add validity checks
return $w('#phone').valid &&
$w('#email').valid &&
$w('#name').valid;
}
function popUpMessage(message) {
wixWindow.openLightBox('Show Message', {'message':message});
}