Duplication of users profile

Good morning,

I hope one of you will be able to help me with this issue.

I’ve had a problem for a few days on my website : I have a dynamic page for members (with member-specific informations) and members must log in to access it.

Since a few days, members connect (always with the same email address) and arrive on a page without all the informations from the database. Indeed, by looking in the database, a new line has been added for them.

The email address is the same, but wix create a new “profile” and they receive a new ID. The following times, they are redirected to this new profile, as if the old one no longer existed.

If I delete it, wix create another one when the member try to connect.

I have not changed anything in the code and therefore see no reason why it should not work.

(My background in coding is close to 0)

Thank you in advance for your help.

Here is the login code :

if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#button27”).label = “LOGIN”;
$w(“#button3”).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("profile") 
      .eq("_id", userId) 
      .find(); 
  } ) 
  .then( (results) => { 
    // if an item for the user is not found 
    if (results.items.length === 0) { 
      // create an item 
      const toInsert = { 
        "_id": userId, 
        "email": userEmail 
      }; 
      // add the item to the collection 
      wixData.insert("profile", toInsert) 
        .catch( (err) => { 
          console.log(err); 
        } ); 
    }

Hey Benjamin,

How about instead of relying on the user’s ‘_id’, try to rely solely on the email? This way even if a user is deleted then registers again, he will be merged into it’s previous record.

What do you think?

Good Luck,
Itai

Hello Itai,

Thank you for your answer.

Actually yes, it would be great. As I’m not a developper and have 0 knowledge (for the moment :slight_smile: ) , I’m not sure of what I have to change in the code (and database ?)

The name of the email column, is Email.

Is this like this ?

// 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(“profile”)
.eq(“Email”, userEmail)
.find();

Should I have to change something in the database ?

And las thing, will the users be able to go back on the correct page (the old one) without having to subscribe again ?

Thanks a lot for the help

Yes, That’s exactly the code you need, except for one small thing. ‘Email’ is the display name of the column, you need the field key, which is ‘email’ (non capital).

I see no reason why it shouldn’t work for already registered users. But you can go ahead and check, just to be sure :slight_smile:

There is many “Emai”, so I’m not sure the one we are tlaking about. Is it the last one like this ? Or others ?

// 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(“profile”)
.eq(" email ", userEmail)
.find();

Thanks a lot

Exactly

Thanks.

I just added the code, but one of my clients just tried to log and I saw again a new line in the database …

It seems that we still have an issue here ^^