Cant get current user email

why isn’t this working???
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

$w.onReady(function () {
	let user = wixUsers.currentUser;
	let email = user.getEmail();
	console.log(email);
	wixData.query("Listings")
		.eq("email",email)
		.find()
		.then((results) => {
			console.log(results.items);
			$w("#table1").rows = results.items; 
		});
		});

You are not waiting for the getemail to be resolved (it´s an async proces). Use:
user.getEmail()
.then((rsemail) => {
userEmail = rsemail;
etc.
and put the query inside the .then((rsemail), so it will only be executed if the email has been resloved.

Good luck.

1 Like

What am I doing wrong with this code? I want to filter results from a collections “email” field that matches the current logged in users email. No errors show but it won’t filter and displays entire collection.

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

$w.onReady(() =>{
let user = wixUsers.currentUser;
let email = user.getEmail();
wixData.query(“PostBids”)
.eq(“email”, email)
.find()
.then(() => {
$w(“#dataset1”).setFilter( wixData.filter()
.eq(“email”, email) );
});
});

What you are doing wrong is exactly what I told you before. Also have a look at this article: https://www.wix.com/code/home/forum/advanced-tips-tricks/promises-promises

I added your suggestion, still won’t filter. Thanks for your time tho I really appreciate it. I’ll just keep playing around with it.

.then((email) => { 
	let userEmail = email; 

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

$w.onReady(() =>{
let user = wixUsers.currentUser;
let email = user.getEmail()();
wixData.query(“PostBids”)
.eq(“email”, email)
.find()
.then((email) => {
let userEmail = email;
$w(“#dataset1”).setFilter( wixData.filter()
.eq(“email”, userEmail) );
});
});

Try this:

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
$w.onReady(() =>{
let user = wixUsers.currentUser;
let email = user.getEmail()
.then((email) => {
let userEmail = email;
wixData.query(“PostBids”)
.eq(“email”, email)
.find()
});
});

Did not check all brackets. It should work now.

1 Like

Hey Thanks So Much!!

It will probably work, but if not, change email in userEmail in the .eq. overlooked it.