Hi,
I am trying to create unique usernames by doing the following,
-
assigning a username
-
checking that this username does not already exist
if already exist, create a new 1 and then check this
else, assign original username
function createUsername(USER) {
var check = false;
while (check === false) {
wixData.query("Teachers")
.eq("username", USER)
.eq("school", session.getItem('schoolID'))
.find()
.then((result) => {
if (result.items.length === 0) { //username does not alredy exist
check = true;
return USER;
} else {
//create new username
if (USER.length === 6) {
USER = USER + 1;
} else {
let userStart = USER.substring(0, 5);
let userEnd = +(USER.substring(5));
USER = userStart + (userEnd + 1).toString();
}
}
})
.catch((err) => {
let errorMsg = err;
});
}
}
The original assigned username (USER) is fed into the function.
The function is then called (not sure if that is the correct terminology).
However, this is not working. Can someone help please?
Thanks