You need to create a function that you can call to check for the email and then return the dynamic page URL you need, from your table.
Here is the sample function you would need to put in a backend jsw file.
import wixData from 'wix-data';
export async function checkUserExists(email) {
if (typeof email !== 'undefined' && email !=="") {
let options = {
"suppressAuth": true,
"suppressHooks": true
};
// convert email to lowercase and remove all spaces
var _email = email.toLowerCase().split(" ").join("");
var url = "";
return wixData.query("Teachers")
.eq('email', _email)
.find( options )
.then( (results) => {
if (results.items.length !== 0) {
// we have an existing record for this email
// only return the first successful row, if
// multiple rows with same email are found
url = results.items[0].teachersProfile(Id); // your fieldname here
return url;
} else {
// there was no database row with this email
return "Error: User not setup. Contact admin for help.";
};
})
.catch( (error) => {
// error occurred trying to read from the table,
// or it does not exist?, or permissions error?
console.log("Error reading from table: Teachers.\n"+error.message);
return "Error: Fetch error trying to get data from table Teachers.";
});
} else {
// error email parameter was not passed in, or is null/invalid
return "Error: Email parameter passed in was invalid.";
}
}
You need to then call this backend function from your code on your page. This also makes it more secure, since the backend code is not accessable by users and hides the table names and field names.
Lets say you call the backend file backend/BE_functions.jsw
In your page code you need to insert at the top of your code a reference to the function and the file location where it can be found in. I didn’t have your complete code, since you only provided a snippet, but this is the idea and should get you up and running.
import { authentication } from 'wix-members';
import wixLocation from 'wix-location';
import {checkUserExists} from 'backend/BE_functions';
let userEmail="";
$w.onReady(function() {
}
export async function loginTeacher_click(event) {
// convert email to lowercase and strip out any spaces
userEmail = $w('#emailLogin').value.toLowerCase().split(" ").join("");
let password = $w('#passwordLogin').value;
try {
await authentication.login(userEmail, password);
console.log("Member is logged in");
let newlink = await checkUserExists(userEmail);
if (newlink.substring(0,6) !== "Error:") {
let realLink = "/teachers/profile/" + newlink;
console.log(realLink);
wixLocation.to(realLink);
} else {
console.log("Error occurred: \n" + newLink);
}
} catch (error) {
console.log(error);
}
}
When looking up values such as emails, and comparing them to values in a database always convert them to one case, either all uppercase or all lowercase. It is a very good idea to have whomever is responsible for entering in the emails and other info into the table, always type in the columns being used for searching, in the same consistent alpha case.
In this way, if users type in various combinations for their email, you can always successfully search for them. Searches are case sensitive, so this will avoid mistakes and failed lookup errors, if your user types in the email address in all uppercase or all lowercase or any combination thereof.
As well I’ve tried to handle the various errors that can occur when using the backend function. Therefore you will see various tests and error messages in the backend function.
Review the function and change the tablename and fieldname to match your exact needs. To get the correct fieldname that you need for your function, go into the content manager, select the table. Click on the column you need the name for and then select properties. The value called ‘Field Key’ is the fieldname you need to use in the function.
I personally don’t like to embed events in the onReady() function. It makes it look messier and harder to follow. I only put initialization code into the onReady() function. Things I need to deal with, once the page has loaded. My personal preference is to use the Wix editor and click on the button object, and then add an on_click() event from within the editor (bottom right). This creates a separate event function and does not embed it within the initial onReady() function. I then edit this newly created event as needed.
I had to add async to the beginning of the event function to let it know that I will be using an await within the function. This await will force the function to pause, until the data from the backend function is returned and available, before continuing to process the remainder of the function.
Good luck.