# Get current user email and add to collection

**URL:** <https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875>\
**Category:** Ask the community\
**Tags:** wix-members-area, code, question\
**Created:** [October 15, 2018, 9:31pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875 "2018-10-15T21:31:32Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![euan-chisholm](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/euan-chisholm/32/1420_2.png) [@euan-chisholm](https://forum.wixstudio.com/u/euan-chisholm)\
**Post date:** [October 15, 2018, 9:31pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/1 "2018-10-15T21:31:32Z")

</div>

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](mailto:user@something.com)”  
} );  
user.getEmail()  
.then( (email) =\> {  
$w(‘#userEmail’).text = email;  
})

How can I post the collected email to the relevant collection?

Thanks

Euan

---

<div class="post-metadata">

**Author:** ![sapirh](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sapirh/32/6655_2.png) [@sapirh](https://forum.wixstudio.com/u/sapirh)\
**Post date:** [October 21, 2018, 2:17pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/2 "2018-10-21T14:17:37Z")

</div>

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](https://support.wix.com/en/article/adding-a-member-login-button-4628013)

 ![](https://us1.discourse-cdn.com/wix/original/3X/e/f/efcc881505f5ac5da8733b7760d63d0c36e0958e.png)

**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](https://www.wix.com/code/reference/wix-data.html#insert)

Have a nice day and best of luck!

---

<div class="post-metadata">

**Author:** ![euan-chisholm](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/euan-chisholm/32/1420_2.png) [@euan-chisholm](https://forum.wixstudio.com/u/euan-chisholm)\
**Post date:** [October 24, 2018, 11:01pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/3 "2018-10-24T23:01:18Z")

</div>

Thank you both managed to sort it, thanks for your help.

---

<div class="post-metadata">

**Author:** ![arber](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/arber/32/6997_2.png) [@arber](https://forum.wixstudio.com/u/arber)\
**Post date:** [November 13, 2018, 2:59am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/4 "2018-11-13T02:59:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arber](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/arber/32/6997_2.png) [@arber](https://forum.wixstudio.com/u/arber)\
**Post date:** [November 13, 2018, 3:00am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/5 "2018-11-13T03:00:28Z")

</div>

It works within scope but not globally

---

<div class="post-metadata">

**Author:** ![jeff-haskins](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jeff-haskins/32/1864_2.png) [@jeff-haskins](https://forum.wixstudio.com/u/jeff-haskins)\
**Post date:** [February 18, 2019, 1:47am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/6 "2019-02-18T01:47:47Z")

</div>

@sapirh 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](mailto: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](mailto:user@something.com)”  
$w(“#dataset5”).setFieldValue(“email”, userEmail);

} );  
}

---

<div class="post-metadata">

**Author:** ![sapirh](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sapirh/32/6655_2.png) [@sapirh](https://forum.wixstudio.com/u/sapirh)\
**Post date:** [February 18, 2019, 8:16am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/7 "2019-02-18T08:16:43Z")

</div>

Hi,

Please explain again what you wish to achieve.  
Send us a URL to your site and specify the page name.

Best,  
Sapir

---

<div class="post-metadata">

**Author:** ![jeff-haskins](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jeff-haskins/32/1864_2.png) [@jeff-haskins](https://forum.wixstudio.com/u/jeff-haskins)\
**Post date:** [February 18, 2019, 1:26pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/8 "2019-02-18T13:26:14Z")

</div>

@sapirh Could not see the forest for the trees. Had been so focused on coding this that missed simplest manner to handle my situation. Since my system set up where user is already in the database, just used datasets to connect a dropdown to their email and use that as form input.

But could use a nudge on how to implement a hide/show button for user. Want to limit access to this page if they have already completed this new input. They can input a personal profile, and a business profile to the database. So already have a function that checks and limits access via their email count so only one personal profile can be set up. So to limit for this business I want to hide/show the button for access to this new creation by querying the type (a field in database)…so if type is business, then they can not see button.

---

<div class="post-metadata">

**Author:** ![barb](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/barb/32/11137_2.png) [@barb](https://forum.wixstudio.com/u/barb)\
**Post date:** [August 16, 2019, 6:29pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/9 "2019-08-16T18:29:36Z")

</div>

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:

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/a/6a5ff7f229c7f7121ce4404cc2f84b902cbed98e.png)  
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](mailto: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.

---

<div class="post-metadata">

**Author:** ![santiago70772](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/santiago70772/32/25754_2.png) [@santiago70772](https://forum.wixstudio.com/u/santiago70772)\
**Post date:** [December 28, 2020, 7:58pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/10 "2020-12-28T19:58:15Z")

</div>

Hello! this work for me

**let** user = wixUsers.currentUser.getEmail()  
.then((email) =\> {  
console.log(email);  
});

---

<div class="post-metadata">

**Author:** ![balwindersingh829](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/balwindersingh829/32/26332_2.png) [@balwindersingh829](https://forum.wixstudio.com/u/balwindersingh829)\
**Post date:** [January 24, 2021, 8:19pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/11 "2021-01-24T20:19:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![csyusufsalman](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/csyusufsalman/32/29792_2.png) [@csyusufsalman](https://forum.wixstudio.com/u/csyusufsalman)\
**Post date:** [October 5, 2021, 5:09am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/12 "2021-10-05T05:09:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![empro2pro](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/empro2pro/32/31056_2.png) [@empro2pro](https://forum.wixstudio.com/u/empro2pro)\
**Post date:** [December 8, 2021, 11:50am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/13 "2021-12-08T11:50:09Z")

</div>

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.

```javascript
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
            });
 });
 
 

```

---

<div class="post-metadata">

**Author:** ![empro2pro](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/empro2pro/32/31056_2.png) [@empro2pro](https://forum.wixstudio.com/u/empro2pro)\
**Post date:** [December 8, 2021, 11:54am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/14 "2021-12-08T11:54:30Z")

</div>

Hi mate. How did you solve this issue when the data was available in the text input field but did not submit to the collection?

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [December 8, 2021, 3:02pm UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/15 "2021-12-08T15:02:26Z")

</div>

@empro2pro

> **[Get current member data and submit in the collection | Velo by Wix](https://community.wix.com/velo/forum/coding-with-velo/get-current-member-data-and-submit-in-the-collection)**
>
> Hi everyone. 
> 
> Have issues with updating a collection with a code. Seen similar topics on this problem but did not find an answer.
> 
> Here is the scenario: the user goes to the page and fills a form. Need to fetch user's email and nickname from...

Own created posts are always the better way to get your answer 😉

---

<div class="post-metadata">

**Author:** ![jicruz302](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jicruz302/32/33903_2.png) [@jicruz302](https://forum.wixstudio.com/u/jicruz302)\
**Post date:** [February 26, 2023, 4:35am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/16 "2023-02-26T04:35:32Z")

</div>

+1 worked for me, thank you for sharing! 🙂

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [February 26, 2023, 9:14am UTC](https://forum.wixstudio.com/t/get-current-user-email-and-add-to-collection/1875/17 "2023-02-26T09:14:52Z")

</div>

Don’t use the old and depricated Wix-Users-API !  
Instead use the new MEMBER-API…

[https://www.wix.com/velo/reference/wix-members](https://www.wix.com/velo/reference/wix-members)

Also do not bump-up old posts, instead open your own new one and describe your own issue. You can link other related possts to your own.  
This is an example why you shouldn’t do that.
