Prevent contact duplicates in custom registration

Hi! I am trying to make sure there is no duplicates mail address in custom registration by using .find().
I got the below error in the “return”, what is the meaning??
TypeError: user.register is not a function at of532.js:21

Here is my code: 

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

$w.onReady(  function () { 
let  user = wixUsers.currentUser; 

$w('#button2').onClick(() => { 
let  usermail =[]; 

    wixData.query("cand_reg")  
        .eq("cand_email", usermail ) //input3 is the box for inputting mail address 
        .find() 
        .then((res) => { 
 if  (res.totalCount > 1) { 
 // Internal Error more than one entry exists 
 **throw**  Error('Internal Error - more than one entry exists'); 
            }  **else if**  (res.totalCount === 1) { 
 **throw**  Error('Duplicate user found, returning'); 
            } 
 //insert to collection here (no duplicate found)  
 **return**  user.register($w('#input3').value, $w('#input5').value, {  //NOT WORKING 
 "cand_reg": { 
 "cand_firstName": $w('#input2').value, 
 "cand_lastName": $w('#input1').value, 
 "cand_email": $w('#input3').value, 
 "cand_birthday": $w('#datePicker1').value, 
 "cand_password": $w('#input5').value, 
                } 
            }); 
        }) 
        . catch ((error) => { 
            $w('#text24').show(); //NOT working: the error messages is always showing 
            console.log(error); 
        }) 
}) 
}); 

New to coding so it will be great if any one have some advises.

I have read the following for reference:
https://www.wix.com/corvid/forum/community-discussion/prevent-contact-duplicates-in-custom-registration

Thank you very much!!!

KANA

What is user.register supposed to be? Are you trying to register the user in this code, then you need to use this: https://www.wix.com/corvid/reference/wix-users.html#register

Plus, you can’t use Wix Users if the user themselves hasn’t actually registered themselves as a member on your site: https://www.wix.com/corvid/reference/wix-users.html

If you want to stop duplicates, then look at the following links and see if their code can help you with your requirements:
https://codequeen.wixsite.com/membership-dashboard
https://www.youtube.com/watch?v=yLCOqsVHhD0&feature=youtu.be (video for above page)
https://www.wix.com/corvid/forum/wix-tips-and-updates/example-no-database-duplicates

Plus, read about before insert data hooks here:
https://www.wix.com/corvid/reference/wix-data.Hooks.html#beforeInsert

1 Like

Yes “user.register” is for register the user and actually it worked fine BEFORE I try to check duplicates . (Reference: https://support.wix.com/en/article/corvid-tutorial-creating-a-custom-registration-form-with-code )

As you said I should not use “wixUsers.currentUser” as they are not yet registered. so I changed to following but result is still the same. If I am not supposed to use “wixUsers.register”, what should I use??

I am still trying to understand the other references you have advised, thank you so much @ givemeawhisk y

~~
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

$w.onReady( function () {
$w(‘#button2’).onClick(() => {
let usermail =$w(‘#input3’).value;
wixData.query(“cand_reg”)
.eq(“cand_email”, usermail ) //input3 is the box for inputting mail address
.find()
.then((res) => {
if (res.totalCount > 1) {
// Internal Error more than one entry exists
throw Error(‘Internal Error - more than one entry exists’);
} else if (res.totalCount === 1) {
throw Error(‘Duplicate user found, returning’);
}
//insert to collection here (no duplicate found)
return wixUsers.register($w(‘#input3’).value, $w(‘#input5’).value, {
“cand_reg”: {
“cand_firstName”: $w(‘#input2’).value,
“cand_lastName”: $w(‘#input1’).value,
“cand_email”: $w(‘#input3’).value,
“cand_birthday”: $w(‘#datePicker1’).value,
“cand_password”: $w(‘#input5’).value,
}
});
})
. catch ((error) => {
$w(‘#text24’).show(); //show error messages
console.log(error);
})
})
});

@kana

Yes user.register won’t work as that is not the correct code, if you look at all examples of the code you will see the correct version is wixUsers.register, exactly like you have done in your second post. The same with wix Data, it should be wixData etc, which you did have correctly.

Anyways, as for the tutorial that you were looking at, well yes you could use that example for detecting duplicates, however that only works after the user is registered.

What would be the best route for this would be for you to use data hooks and add a before insert hook which checks for any duplicated email addresses before the users contact info gets added.

The same can be done for a members profile page where you could use before update if they are wishing to change their email address.

Have a read of these pages for more info:
https://www.wix.com/corvid/forum/community-discussion/search-for-duplicate-entires
https://support.wix.com/en/article/corvid-using-data-hooks

Similar to what Nayeli (Code Queen) has done for duplicate usernames in this code example:
https://codequeen.wixsite.com/membership-dashboard/the-backend-code

1 Like

Hi @givemeawhisky

Thanks to the references (Code Queen) you have given me, I managed to use data hooks to prevent same mail address!!! :slight_smile:

However, the mamber information is now inserted in my collection “cand_reg” but not
“PrivateMembersData” so the user can not log-in to their page.

I will have a look of other references these days.