Please help! Custom Login won't select the correct logged-in user for dynamic pages.

I have custom-login process as per one of the tutorials, where you have a custom collection, a dynamic profile page, and a dynamic profile edit page. It worked perfectly with one user, but once I get another user, it feeds the correct data into the correct place in the “Members” collection, but when I log-in using the latest users email etc.

The site itself if logged in with my current ID, but the ID that is being referenced by the dynamic datasets is from the latest user. In the image, this is my ID generated by updating the text box from User.id, the bottom ID is pulled from the collection via a Database connection (they should be the same). Code to follow:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( function () {
if (wixUsers.currentUser.loggedIn) {
//update buttons to Logout & show Profile
$w(“#buttonLogin”).label = “Logout”;
$w(“#buttonProfile”).show();
}
else {
//update buttons to Login & hide Profile
$w(“#buttonLogin”).label = “Login”;
$w(“#buttonProfile”).hide();
}

});

export function buttonLogin_click(event) {

//if user is logged in
if (wixUsers.currentUser.loggedIn) {
//re-direct to logged out page (if applicable)
wixLocation.to(“/aboutyoufitness”);
//and log them out
wixUsers.logout()
//then update buttons accordingly
.then( () => {
$w(“#buttonLogin”).label = “Login”;
$w(“#buttonProfile”).hide();
});
}
else { // if user is logged out start logging them in
//set variables
let userId;
let userEmail;
//add other variables here;

//prompt user to log in
wixUsers.promptLogin( {“mode”: “login”})
//then assign values to allow checking for duplicates
.then ( (user) => {
userId = user.id;
return user.getEmail();
})

//then check if there is an item with the same email already in the collection
.then ( (email) => {
userEmail = email;
return wixData.query(“Members”) //“name of the collection”
.eq(“_id”, userId)
.find();
})
//if no user found then collect data and insert into database
.then( (results) => {
//if results don’t show any entries with the same email
if (results.items.length ===0) {
//create an item to be inserted in collection (database)
const toInsert = {
“_id”: userId,
“title”: userEmail, //field in the collection : variable being passed
//add others here if applicable
//“firstName” : “First Name”,
//“lastName” : “Last Name”,
//“phone” : “Phone Number”
};

//insert blank fields for the workouts
const insertBP = {
“title”: userEmail,
“exerciseType” : “ae9510d2-e1f1-44e0-b16e-f756dc477eaf”,
//“exerciseOwner” : userEmail,
“exerciseGoal” : 0,
“exercisePb” : 0,
// “exercisePrevious”: 0
};
const insertBS = {
“title”: userEmail,
“exerciseType” : “77757450-df66-4086-9241-3b8835bd4180”,
//“exerciseOwner” : userEmail,
“exerciseGoal” : 0,
“exercisePb” : 0,
// “exercisePrevious”: 0
};
const insertDL = {
“title”: userEmail,
“exerciseType” : “7938c07a-861f-4953-93c0-b9ffb6cb0dad”,
//“exerciseOwner” : userEmail,
“exerciseGoal” : 0,
“exercisePb” : 0,
//“exercisePrevious”: 0
};

    wixData.insert("Members",toInsert) 
      . **catch**  ((err)=> { 
        console.log(err); 
      }); 
    wixData.insert("MembersExercises", insertBP) 
      . **catch**  ((err)=> { 
                    console.log(err); 
                }); 
            wixData.insert("MembersExercises", insertBS) 
      . **catch**  ((err)=> { 
                    console.log(err); 
                }); 
            wixData.insert("MembersExercises", insertDL) 
      . **catch**  ((err)=> { 
                    console.log(err); 
                }); 
    } 

//re-direct to the profile page
//wixLocation.to(/Members/{Login Email});

//update buttons accordingly
$w(“#buttonLogin”).label = “Logout”;
$w(“#buttonProfile”).show();
$w(“#userTextID”).text = userId;
} )
. catch ( (err) => {
console.log(err);
} );

}

}