So I’ve been having an issue where user’s login emails keep coming back as undefined even though I can get their IDs and other member information. I’ve also had issues getting IDs from the _id field of various databases to populate.
Here’s the code I’m using for the simple action of getting a login email:
var user = wixUsers.currentUser;
var isLoggedIn = user.loggedIn;
var userId = user.id;
var userEmail = user.loginEmail;
console.log("User email is " + userEmail)
console.log("User ID is " + userId);
When using this I can get their ID but not their email. It always comes back as undefined in the console.
Additionally, when I try to use a query to get information, the _id field always comes back undefined:
wixData.query(“2020_Registration”)
.eq(“pmdUserId”, userId)
.find()
.then( (results) => {
let items = results.items[0];
let ttId = items[0].id;
console.log("The user’s ID for this DB is " + ttId);
…
I’ve tried items.id, items[0].id, items._id, and items[0]._id and none of them seem to work. Oddly enough, it works on some databases and not others. I’ve even set all permissions to anyone on the database to test if it’s a permission issue and still no luck.
Any help would be greatly appreciated.
Take a look at the Wix User api.
https://www.wix.com/corvid/reference/wix-users.html#currentUser
https://www.wix.com/corvid/reference/wix-users.User.html#id
https://www.wix.com/corvid/reference/wix-users.User.html#getEmail
As for the query, that should just be eq for the user id.
https://www.wix.com/corvid/reference/wix-data.html#query
https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#eq
Something like this for example.
wixData.query("Members")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// And so on with the code.....
Thanks for the reply. I’ve used the getEmail() code as well and got the same result of email being undefined. I was also concerned because the results of getEmal() would only be usable within that block. Oddly enough, if I query the private members database I can get the email without issue but the page where I use this has that query at the start of the page so it doesn’t cause any issues with var being relegated to a particular block or scope.
As for the other query, I actually have multiple databases for each member (something I did to avoid issues I’m having with update code) so I actually put the member’s private members id into each of my databases. This is why my query is utilizing the userId in databases other than the members database. The problem is that within the results I can get the ID of that particular database to appear but it comes up undefined when I try to assign it to a variable, thus keeping me from actually being able to update the database.
I’ve since restructured the way databases work on my site so I’m no longer having the issues mentioned above.
Thank you for your help.
@dwyanekrzanowski
I’m experiencing this same issue with the current user email returning as undefined. I’ve used every which way, including the recommended getEmail() function, and still no luck. I understand that there is a known issue with testing the retrieval of this info in preview mode, so I’ve been doing the tedious task of publishing and refreshing and testing the live site and all to the same result of email being undefined.
What was your work around for this?
Sorry to hear it Brett. To be honest, I still get this error from time to time and it seems to be completely random. I can literally copy the working code from one page to another and have it not work on the new page.
One thing that seems to work for me though is to query the private user database at the start of my onReady code. The problem with this, though, is that in order to use any of the query results you’ll have to nest the remainder of your page code within the .then of the query.
I’m not a trained programmer but I don’t think this is always the best way to go about it.
Anyway, below is a sample of the code I pulled from my page where I got it to work. I hope it helps you.
import wixUsers from ‘wix-users’;
$w.onReady( function () {
let user = wixUsers.currentUser;
let userId = user.id; //used in the upcoming query
console.log(“User ID is " + userId); //just to make sure it worked
let userEmail = user.loginEmail; //this never works for me, nor did getEmail() so you can delete it
wixData.query(“Members/PrivateMembersData”)
.eq(”_id", userId)
.find()
.then( (results) => {
let item = results.items;
let eM = item[0].loginEmail;
//the rest of your code must now be within this .then in order to access the declared variable for the loginEmail
})
.catch( (err) => {
let errorMsg = err
})
I appreciate your reply and info!