Using email address to identify a member

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

}

" Is this a bug or a programmer error? "
This issue has come up many times and every time the cause is the last one. It happens when the field name is not inserted literally. So check the field name (Properties in Content Manager, the grid) and do not confuse the column LABEL with the column NAME (but if you did: we all did at some point).

Ok thanks for the information. I was able to find the correct column name. But I am still getting a duplicate record but at least the email address is in the correct field. What I am doing is the following:

  1. Imported data into my owners table
  2. Create the owners login (using the Wix sign-up process)
  3. View the live data and see there is a duplicate record (all functionality works at this point, but its pointing to the record created during the sign-up process, not the imported record)

What I tried is adding a next record button (hoping that is would find the 2nd), but the site does not see the 2nd record. So I am stuck at this point.