Thank you for your advice. I think I uncovered one possible problem: /Alumni_Members/MemberInfo/${ wixUsers.currentUser.id } gets converted in the system to /Alumni-Members/… (underscore to minus character).
So I think that is fixed.
In the preview function the correct record is now found but when published the system goes back to the top record on file.
I tried moving the export function profileButton code from the bottom of the script to the top - nothing changed.
I then inserted a known userId code (hard wired, I thought) in various places in the script and this works in PREVIEW but not published mode
In the published mode the following debug button shows the correct user ID:
$w(“#button6”).label = wixUsers.currentUser.id; //control
but
hovering over the #profileButton shows up a reference to the top record in the database, not the linked record.
The button code settings:
Connect dataset: Alumni_Members dataset
Label Connects to: Not Connected
Link Connects to: Alumni_Members MemberInfo (ID)
Observation:
In preview mode the link seems to work but not when published.
The control/debug button contents show the correct userId in both preview and published mode.
I am also a bit confused by the example at Velo Tutorial: Building Your Own Members Area | Help Center | Wix.com. I have relied heavily on this to set up this system.
It used the ID as a key in the Members/Update/ID reference but the ‘Connect to Data’ Button connects to the Members Item database and then links to Members (Email). I would have thought that the link should be to Members (ID) if the ID is used as the link field.
Any thoughts?
Many thanks again for the help.
Regards,
Klaus
----- Page Code below ----------
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
let userId;
let userEmail;
export function profileButton_click(event, $w) {
wixLocation.to(“/Alumni-Members/MemberInfo/25ade2ce-ff56-4d3b-8979-2b6dccefae76”); // inserted kfelsche@ozemail ID - still defaults to first record in database in published mode
// wixLocation.to(“/Alumni-Members/MemberInfo/${wixUsers.currentUser.id}”); // moved inside the sub-routine
$w(“#button6”).label = wixUsers.currentUser.id; //control
}
$w.onReady(() => {
if (wixUsers.currentUser.loggedIn) {
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
$w(“#button6”).label = wixUsers.currentUser.id; //control
} else {
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
}
});
export function loginButton_click_1(event, $w) {
// user is logged in
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then(() => {
// update buttons accordingly
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
wixLocation.to(“/Alumni-Members/MemberInfo/25ade2ce-ff56-4d3b-8979-2b6dccefae76”); // inserted kfelsche@ozemail ID
//wixLocation.to(/Alumni-Members/MemberInfo/${wixUsers.currentUser.id}
); // moved inside the sub-routine
});
}
// user is logged out
else {
// 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(“Alumni_Members”)
.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(“Alumni_Members”, toInsert)
.catch((err) => {
console.log(err);
});
let userId = wixUsers.currentUser.id;
wixLocation.to(“/Alumni-Members/MemberInfo/25ade2ce-ff56-4d3b-8979-2b6dccefae76”); // inserted kfelsche@ozemail ID
// wixLocation.to(“/Alumni-Members/MemberInfo/${wixUsers.currentUser.id}”);
}
// update buttons accordingly
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
})
.catch((err) => {
console.log(err);
});
}
}