Hi Philipp:
Per our offline discussion. If you change line 42 in your code example above to .eq(‘eMail’, email) then that will at least bind the logged in user from wix-users/wix-crm to your TeacherProfiles data collection.
Once you have the results from the TeacherProfiles data collection you need to use that information to drive your User Profile button code so it is probably a good idea to remember what was retrieved from the find() request by adapting the promptLogin result like so:
//************************************************
// Add this code to remember the found record! //************************************************
let teacherProfileRecord = null;
export function button1_onclick() {
// user is logged in
teacherProfileRecord = null; // reset the teacherProfile record
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#button1").label = "Login / Register";
$w("#button2").hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
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("TeacherProfiles")
.eq("eMail", email)
.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("TeacherProfiles", toInsert)
//************************************************
// Add this code to remember the found record!
//************************************************
.then((insertedRecord) => {
// Enable member actions
loginSuccessful(insertedRecord);
})
.catch( (err) => {
console.log(err);
} );
//************************************************
// Add this code to remember the found record!
//************************************************
} else if (results.items.length === 1) {
// Enable member actions
loginSuccessful(results.items[0]);
}
} )
.catch( (err) => {
console.log(err);
} );
}
}
//************************************************
// Add this code to remember the found record! //************************************************
function loginSuccessful(teacherProfile) {
if (teacherProfile) {
// Remember the user record for dynamic page generation
teacherProfileRecord = teacherProfile;
// update buttons if successful
$w("#button1").label = "Logout";
$w("#button2").show();
}
}
Now to load the dynamic pages you can just get the page URL from the TeacherProfile record like so
export function button2_onclick() {
if (teacherProfileRecord && teacherProfileRecord['link-TeacherProfiles-Update']) {
wixLocation.to(teacherProfileRecord['link-TeacherProfiles-Update']);
}
}