I am trying to use an email address to identify an member. I created a database called owners, imported data into the owners database. When I register as a new user using an email address that exists in my database a duplicate record is created in the owners database.
The code on my login screen is below. The database is called owners and the email field is Own_Email. I would expect that the insert would not happen since the email address I registered under is in the Owners Database in the Own_Email field. When I look at the owners database there are now two fields called “Own_Email”. Is this a bug or a programmer error?
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady(() => {
if (wixUsers.currentUser.loggedIn) {
$w(“#login”).label = “Logout”;
$w(“#profile”).show();
} else {
$w(“#login”).label = “Login”;
$w(“#profile”).hide();
}
});
export function login_click() {
// user is logged in
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then(() => {
// update buttons accordingly
$w(“#login”).label = “Login”;
$w(“#profile”).hide();
});
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin({ “mode”: “login” })
.then((user) => {
userId = user.id;
return user.getEmail();
})
.then((email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query(“Owners”)
// .eq(“_id”, userId)
.eq(“Own_Email”, userEmail)
.find();
})
.then((results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
“_id”: userId,
“Own_Email”: userEmail
};
console.log('email not found')
// add the item to the collection
wixData.insert(“Owners”, toInsert)
. catch ((err) => {
console.log(err);
});
}
console.log('email was found')
// update buttons accordingly
$w(“#login”).label = “Logout”;
$w(“#profile”).show();
})
. catch ((err) => {
console.log(err);
});
}
}
//export function button43_onclick() {
// wixLocation.to(/TEST_Database/Update/${wixUsers.currentUser.id}
);
//}
export function profileButton_click() {
//Add your code for this event here:
let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49”
let isLoggedIn = user.loggedIn; // true
let userRole = user.role; // “Member”
user.getEmail()
.then( (email) => {
let userEmail = email; // “user@something.com”
wixLocation.to(“/OwnersUpdate/”+userEmail);
} );
}