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.
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.
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.....