Hi @Paul Krzyz
There was something wrong the field key, even though I copied and pasted it from the database, it wouldn’t go throught, so I created a different ID with phone number. I will paste it below, and the code worked. However the code on my login page still isn’t working. It’s giving me error -19976.
The code from my backend:
import wixData from ‘wix-data’ ;
export async function checkUserExists ( email ){
if(typeof email !== ‘undefined’ && email !== “” ){
let options = {
“suppressAuth” : true ,
“suppressHooks” : true ,
};
//convert to lowercase and remove spaces
var _email = email . toLowerCase (). split ( " " ). join ( "" );
var url = "" ;
**return** **await** wixData . query ( "Teachers" )
. eq ( 'email' , _email )
. find ( options )
. then ( ( results ) =>{
if ( results.items.length != 0 ) {
//returns the first row, if multiple rows with the same email are found
url = results.items [ 0 ]. phoneNumber ;
**return** url ;
} **else** {
**return** "Error: User not setup. contact admin for help." ;
}
})
. catch (( error ) =>{
console . log ( "Error reading from table: Teachers.\n" + error.message );
**return** "Error: Fetch error trying to get data from table Teachers" ;
});
}
**else** {
**return** "Error: Email parameter passed in was invalid"
}
}
code from my login page:
import wixUsers from ‘wix-users’ ;
import wixLocation from ‘wix-location’ ;
import { checkUserExists } from ‘backend/login_functions’
let userEmail = “” ;
$w . onReady ( function (){
})
/**
- Adds an event handler that runs when the element is clicked.
Read more - @param {$w.MouseEvent} event
*/
export function loginTeacher_click ( event ) {
// This function was added from the Properties & Events panel. To learn more, visit Velo: Working with the Properties & Events Panel | Help Center | Wix.com
// Add your code for this event here:
let email = $w ( “#emailLogin” ). value ;
let password = $w ( “#passwordLogin” ). value ;
userEmail = email . toLowerCase (). split ( " " ). join ( “” );
wixUsers . login ( email , password )
. then ( async () => {
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 );
});
}
This is some information from my database:
This is the field key for Teachers Profile (phone number) - link-teachers-title
I just used the field key for Phone number, which was phoneNumber and got the number and added it to the /teachers/profile
