Register + saving data to the collection

Hey!

Do you know how could I custom a registration form + update the information on the database?

I tried with the Wix function (connecting each field to the database and to submit) but its not working.

Right now I have this:

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function(){  
$w('#register').onClick(function (){
 
let updates = {
"companyName": $w("#companyname").value,
"email": $w("#email").value,
"password": $w("#password").value,
"title": $w("#name").value,
"_id": wixUsers.currentUser.id
};

  wixData.save("Company", updates) 
  .then( (results) => {
    wixLocation.to("/welcome");
  } )
  .catch( (err) => {
 let errorMsg = err;
  } );

 let email = $w('#email').value; 
 let password = $w('#password').value;

  wixUsers.register(email,password,{
 "contactInfo": {
 "title": $w('#name').value,
 "companyName": $w('#companyname').value,
      }
    })  
    .then( ()=>{
      wixLocation.to('/welcome'); 
       })
   })
})

But I also tried this:

 
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixData from 'wix-data';

$w.onReady(function(){  
$w('#register').onClick(function (){
 let email = $w('#email').value; 
 let password = $w('#password').value;

  wixUsers.register(email,password,{
 "contactInfo": {
 "title": $w('#name').value,
 "companyName": $w('#companyname').value,
      }
    })  
    .then( ()=>{
      wixLocation.to('/welcome'); 
       })
   })
})

export function register_click(event, $w) {
let updates = {
"companyName": $w("#companyname").value,
"email": $w("#email").value,
"password": $w("#password").value,
"title": $w("#name").value,
"_id": wixUsers.currentUser.id
};

let req1 = $w("#companyname").valid;
let req2 = $w("#email").valid;
let req3 = $w("#password").valid;
let req4 = $w("#name").valid;

if (req1 === false || req2 === false || req3 === false || req4 === false) 
$w("#errorText").show();
else 
{wixData.save("Company", updates)
.then(() => {
//go to welcome page 
wixLocation.to("/welcome");
})
.catch( (err) => {
console.log(err);
} );
}}

Because I read somewhere that they had problems with fields that had the required option.

Thanks!!

1 Like

Can someone help me please??:frowning:

Hi Blai,

It would good to get more details - what exactly does not work? Saving? Registration? Both? In the Preview mode/published site?

Here you can find detailed guide on configuring Custom Registration form - Velo Tutorial: Creating a Custom Registration Form with Code | Help Center | Wix.com,
Also the API reference for the register() function can be useful - Page Not Found - Velo API Reference - Wix.com

As for your code - you might have few issues:

  1. “_id”: wixUsers.currentUser.id line can throw an exception if there will be no logged in user (apparently that is the case - we are talking about the Registration form, right?)
    Try to remove this line. You can get the new user ID in the result returned by the register() function.

In general, to debug the code - wrap it within try-catch block and print out the error in occurs. That is:

$w.onReady(function(){ 
  $w('#register').onClick(function (){
     try{
        .... your code 
     } catch (error) {
        console.error(`Error occurred: ${error}`);
     }
  })
})
  1. Note that the register() function may work only in Published site mode, in in the Preview mode.

  2. Try to add .catch() blocks to both results - from the registration and from the save - and print out the error if occurs.

  3. Despite the fact the requests for register and save are sent in parallel (they are asynchronous), it might be a good idea to perform the redirect only after both operations are completed. (It can be achieved by nesting register request into the .then of the save one, or other way around, or by waiting for both promises)

  4. Make sure you grant sufficient permissions to write data to the Company collection.