Hi there,
I want to get the currently logged-in user email and insert that in a collection called “exam” for this I have added a module in the backend and calling it from the frontend but it is returning the blank object.
backend code (data.jsw)
export function getEmail () {
return wixUsersBackend . currentUser . getEmail (). then (( result ) => {
console . log ( result )
console . log ( result: ${ result }
);
return result ;
})
}
frontend code
import { getEmail } from ‘backend/data.jsw’ ;
let userEmail = getEmail ();
let newQuizTaker = {
‘email’ : userEmail ,
‘attempt’ : 5 ,
‘score’ : 0
}
But it is returning empty objects for the email field
Please help I am unable to fix this issue
You do not —> AWAIT on frontend.
By the way, you do not need to code on backend, to get the email of current logged-in-user.
You can do it also on front-end.
The only advantage of the back-end-version is the higher security i think.
$w.onReady(async ()=>{
let userEmail = await getEmail();
let newQuizTaker = {'email' : userEmail, 'attempt': 5, 'score': 0}
});
Hi there, I tried what you said but can you give me a simple example to define getEmail in backend which return email of user, and in frontend I call that method.
This is what you will need at the back-end:
BACK-END:
import wixUsersBackend from 'wix-users-backend';
export function get_userData(){
let userEmail
let user = wixUsersBackend.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
return user.getEmail()
.then(async (email) => {
userEmail = await email;
return({"ID": userId, "email": userEmail, "state":isLoggedIn})
});
}
And this one goes to FRONT-END:
import {get_userData} from 'backend/data.jsw'
$w.onReady(async function() {
let userData = await get_userData(); console.log("User-Data: ", userData);
}
Good luck!