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.
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);
})
})
});
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.