Hi,
Probably really simple but I'm new to Wix Code and Javascript.
Can anyone either tell me how or point me in the direction of a tutorial that might help with this relatively simple scenario:
I want to collect a logged in user's email address so that I can sync to Wix CRM custom fields via Wix Automations.
I have a members only form that logged in users complete - I can see the user ID in the collection the form is linked to but not the email. I can display the email address in a text field using this code:
import wixUsers from 'wix-users'; // ... let user = wixUsers.currentUser; let userId = user.id; // "r5cme-6fem-485j-djre-4844c49" let isLoggedIn = user.loggedIn; // true user.getEmail() .then( (email) => { let userEmail = email; // "user@something.com" } ); user.getEmail() .then( (email) => { $w('#userEmail').text = email; }) How can I post the collected email to the relevant collection?
Thanks
Euan
Hi guys. Have the same problem and cannot solve it: required data is shown in the text input field, but data is not submitted to the collection when the button is pressed. PLease advise what do I do wrong.
import wixUsers from 'wix-users'; import wixData from 'wix-data'; let user = wixUsers.currentUser; let userId = user.id; user.getEmail() .then( (email) => { let userEmail = email; wixData.query("Members/PublicData") .eq("_id", userId) .find() .then((results) => { console.log(results) let items = results.items; let lN = items[0].nickname; $w('#input6').value = lN; $w('#input8').value = userEmail; //this data is not submitted }); });
I'm using Google authentication to log in the user. The proposed codes doesn't work, I cannot get the user's email address. loggedIn property of the currentUser returns true so I'm pretty sure that the user is logged in but getEmail returns nothing. Is this something related to Google authentication?
let userEmail; let user = wixUsers.currentUser; $w.onReady(async function () { await user.getEmail() .then( (email) => { userEmail = email; }); console.log(userEmail);
}); I have faced same issue Now I am able to get logged in user email
Hello! this work for me
let user = wixUsers.currentUser.getEmail() .then((email) => { console.log(email); });
I am puzzled because I have been able to add the user's login email to one of my data collections (where there is one record per user). But now it isn't working on my new data collection that has multiple records per user (logging hours and miles). At the top of the page, they can add new entries, one by one. In the middle of the page, they can edit individual entries by paging through them until they find the one they want to change. At the bottom is a view-only table of all entries. A bit clunky but I haven't been able to figure out a better way without going into extensive coding (I am not a programmer).
Here's the page:
I have 3 versions of the data collection defined to do this (first is Write only for adding new record, second is Read-Write where user uses Next and Previous to page through entries, third is Read only for the table). The second two are filtered by logged in Owner ID. All of this works great.
When the user chooses an activity (required), their user login email displays in input field 31 (below the Activity drop down at the top of the page) which is linked to the data item LoginEmail (which is for my info as a web admin so I know which OwnerID goes with which member's email). I can see the user's email on the screen when I select the activity, but when I submit the entry, it is not being added to the data collection.
Here's the code:
import wixUsers from 'wix-users'; import wixData from "wix-data"; //When user enters or changes Activity (which is required), their logged in user email is added to the input31 field. Input31 field is connected to the data collection field LoginEmail. Why isn't it updating when user clicks Submit?
export function dropdown1_change(event) { let user = wixUsers.currentUser; let userId = user.id; let isLoggedIn = user.loggedIn; let userRole = user.role; user.getEmail() .then( (email) => { let userEmail = email; // "user@something.com" $w('#input31').value = userEmail; } ); } //Can't remember why this is here but without it, user can't add new entries. export function text219_click(event) { $w('#group1').expand(); } //Refresh data sets when adding new entries (to be able to edit or see them in the table) export function button7_click(event) { $w('#dataset3').refresh() $w('#dataset1').refresh() }
I'd sure like to know what I'm missing.
PS: The other thing I'm trying to figure out is how to total the hours and miles for the user in the table. I'm stumped! Have tried several things but to no avail.
Hi,
Please explain again what you wish to achieve.
Send us a URL to your site and specify the page name.
Best,
Sapir
@Sapir (wix) newbie here. I have tried and tried to get something like this to work, but getting nowhere. My latest effort.
import wixUsers from 'wix-users'; import wixLocation from 'wix-location'; let user = wixUsers.currentUser; let userId = user.id; // "r5cme-6fem-485j-djre-4844c49" let isLoggedIn = user.loggedIn; // true let userRole = user.role; // "Member" user.getEmail() .then( (email) => { let userEmail = email; // "user@something.com" $w("#input4").value = userEmail; } ); } export function button1_click() { let user = wixUsers.currentUser; let userId = user.id; // "r5cme-6fem-485j-djre-4844c49" let isLoggedIn = user.loggedIn; // true let userRole = user.role; // "Member" user.getEmail() .then( (email) => { let userEmail = email; // "user@something.com" $w("#dataset5").setFieldValue("email", userEmail); } ); }
let user = wixUsers.currentUser;
let userEmail;
user.getEmail() .then( (email) => { userEmail = email;
} );
userEmail undefined, I put it in an if statement if(isLoggedIn), I get user id but email undefined
Thank you both managed to sort it, thanks for your help.
Hi Euan,
First of all, in order to get the current user’s email you have to ensure that there is a member who is logged to your site.
You can use the built in member system.
https://support.wix.com/en/article/adding-a-member-login-button-4628013
Now you can use the function getEmail():
let user = wixUsers.currentUser;
let userEmail;
user.getEmail() .then( (email) => { userEmail = email;
} );
Pay attention!
create the variable “userEmail” at the global scope in order to use it outside the function’s scope.
Now, in order to add the “userEmail” field to the collection, use the function insert().
You can read about the function in the link below:
https://www.wix.com/code/reference/wix-data.html#insert
Have a nice day and best of luck!