user.getEmail() What am I wrong ?

$w.onReady( function() {
let userEmail = wixUsers.currentUser.getEmail();
wixData.query(“Members”)
.contains(“email”, userEmail )
.find()
.then( (results) => { console.log(results); } )
// gestione dell’errore
.catch( (err) => { console.log(err); } );
} );


Hi,
I’am in case a member’s just logged in and I would, querying the database particulary the field “email”, do consequentially something

If member logged is Mauro => do something_1
If member logged is Francesco => do something_2

So to debug my code I put the console.log(results) in order to verify the behavior, but the output is:

« Error: Failed to perform query on [Members].
Invalid .contains parameter value [Promise]. .contains parameter must be a String. »

In what am I wrong ?
I have to say that it’s few time I’m learnig Javascript in order to manage Wix Code
Thanks in advance
Mauro

OK I found that each Promise have to finish itself before pass its value.
But How have I pass the value of 1st Promise (getEmail) to the next step ?

Hi Mauro,

You can call “.then(function (userEmail) { … })” on the promise returned from “getEmail” and move all the following logic inside that function.

Alternatively, I think it would be easier to grasp if you covert the code to using async/await .
That way you’ll get nicer code like the below, that looks linear and “hides” the complexity introduced by the asynchronous nature of these operations.

$w.onReady(async function() {
	let userEmail = await wixUsers.currentUser.getEmail();
	try {
		let results = await wixData.query("Members")
  			.contains("email", userEmail )
  			.find();
  		console.log(results);
	catch (err) {
  		// gestione dell'errore 
  		console.log(err);
	}
}  );

Hope this helps!
Ofer

1 Like