Unable to use getEmail() outside of promise handler

Hi

I’ve had a look through the forum but cannot find anything that helps with my specific problem. All I am trying to do is get the email address of the current user and utilise it for a query. After many hours of trying I have set up the code below to try and test what is happening. It attempts to read the user data and post it to several text fields on a page:

// Set up the user information as variables for this function
var userEmail = “”;
var userId = “”;
var userLoggedIn = false;
var userRole = “Visitor”;

// Gets the user information  
let user = wixUsers.currentUser; 

// Now assign the user information to the variables declared above 
userId = user.id; // assign the ID 
userLoggedIn = user.loggedIn; // assign logged in info 
userRole = user.role; // assign the user role 
user.getEmail() 
  .then((email) => { 
    userEmail = email;  // assign the email 
    $w("#text20").text = userEmail;	// this line checks userEmail has the email address 
}); 

// Put user information taken from the current user onto the screen 
$w("#text17").text = userId ;  // this works fine the user id is stored 
$w("#text18").text = userRole ; // this works fine the user role is stored 
$w("#text19").text = userEmail ;  // No sign of the user Email anymore it has disappeared 

My question is how can I make sure I have the email address of the user stored in a variable I can use when the .then(email) promise handler ends?

many thanks,
James

You need to access userEmail inside the .then from the Promise. Like this:

	user.getEmail()
	  .then((email) => {
	    userEmail = email;  // assign the email
	    $w("#text20").text = userEmail;	// this line checks userEmail has the email address
            $w("#text19").text = userEmail ;  // <======
	});

Code that appears after the .then of the Promise most probably will execute before the Promise is fulfilled. So, it’s not that is has “disappeared”, rather it probably hasn’t been retrieved yet.

Thanks Yisrael. I am not very familiar with the workings of Promises. My actual need was not to set the .text on the screen (I was just using that as a debugging device) but to use the email address to query a database. However I embedded the query etc. in the same way as above and it works fine. (Posting the code below in case it’s of use to anybody).

user.getEmail() 
	.then( (email) => { 
		userEmail = email;      // "user@something.com" 
		$w("#text19").text = userEmail; 
		  // Now we have an email address  
   		  // Run a query on the collection 	 
    	wixData.query('database1') 
		  // Query the collection for any items whose "email" field contains the userEmail 
			.contains('db_user', userEmail) 
			.find() // Run the query 
			.then(res => { 
				// Set the table data to be the results of the query      
				$w('#table1').rows = res.items; 
			}); 
}); 

This now pulls data items from database1 and populates a table with items where the email stored in db_user is equal to the current users email address.

Glad it worked out. Just sort of give you the warm fuzzies - right?