How to add the user email to database?

I have a custom form that submit to a database, I want to add the email of the user logged in when the user click on the submit buttom.

I achive to do that on the preview mode, but doesn’t work live.
I use this code to get the user email and post it on an input field, that input field is connected to the database. And when the user submit the form, also submit the email. But only works on preview mode.

import wixUsers from 'wix-users';

let user = wixUsers.currentUser;

let userId = user.id;
let isLoggedIn = user.loggedIn;

user.getEmail()
    .then((email) => {
 let userEmail = email;
    });
user.getEmail()
    .then((email) => {
        $w('#input7').value = email;
    })

Hello FX-Agency,

i do not see any piece of code here, which says…
“put the founded e-Mail into my database!”

And i do not know, if this here is correct, too…
$w ( ‘#input7’ ). value = email ;
As i can see you try to put an value into an “INPUT-FIELD” ?
Does it make sense?

I think you want to show the founded eMail in a textfiled or something like that
and of course futher you want to write/save it into your database (data-collection).

i know 2-ways to realize your project.

$w("#myDataset").setFieldValue("title", "New Title");
$w("#myDataset").save();
import wixData from 'wix-data';

// ...

let toInsert = {
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.insert("myCollection", toInsert)
  .then( (results) => {
		let item = results; //see item below
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );

I did the submit function with a button connected to the dataset. Not with code. I put the email founded on a input field connected to the dataset.

Sorry, i do not know how to realize it without code.

I can try to do the code, I just need a little help :slight_smile:
I will try the second option. Where it say"title" I have to put the fieldkey of the field of my database?

Hello,

i think this part here is easy to understand…

let toInsert = {
  "title":        "Mr.",      //<--- first column in your DB
  "first_name":   "John",     // DB = Database (data-collection)
  "last_name":    "Doe"
};

With this code you generate the value, which later will be written into your Data-collection (Database).
On the left side —> the colum-IDs and on the right side your values!

And here…

wixData.insert("myCollection", toInsert)

…just put in the name-ID (not the name!) into the field which is marked blue here.

All what is marked BLUE —> User-changeable!

Sorry, it’s very late now and my brain has a loss of concentration :grin:

Lets gonna start from zero, please :frowning:

I have three input fields and and a buttom to submit. All this works normally connecting to the database, following a tutorial like this https://support.wix.com/en/article/creating-a-form-with-user-input-elements but I need to identify who submit that information. That’s why I tried to achive with that weird code above.

I think making this process https://support.wix.com/en/article/creating-a-form-with-user-input-elements but with code, it will works. Thanks for your help and paitience :pray:

First, always test Wix Users API on a live published site.
https://www.wix.com/corvid/reference/wix-users.html
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

Also, make sure that you have your data mode and permissions setup correctly.
https://support.wix.com/en/article/working-with-dataset-modes-and-collection-permissions

If you simply want to get the users email, then setup a user input as read only and hidden on load, so that only you will know that it is on the page.

Then you can simply get the current users email and add it to that box so that you have the email address pre-filled out for you.

This is already mentioned in the Wix Users API, with an example for doing it with the users lastname, so you just need to change it to the user email which would be email.

Use the currentUser property to get the current user’s id. Then query the Members/PrivateMembersData collection for the item with that _id.

wixData.query("Members/PrivateMembersData") \
  .eq("_id", wixUsers.currentUser.id) \
  .find() \
  .then( (results) => { \
    lastName = results.items[0].lastName; \
  } );

See this previous forum post for a complete breakdown of what to do and code example for you.

Once you have added that code to your page, then all you need to do is to set the field value so that it knows to add that to the dataset field.

As the user is not adding any value to the user input and you are simply adding it through code, the user input does not register any value inputted, hence why you need to set the field through code.

If you are using a submit button to save the submissions to a dataset, then you need to do nothing else.

I think we’re getting close

import wixUsers from 'wix-users';
import wixData from 'wix-data';
wixData.query("Members/PrivateMembersData")
            .eq("_id", wixUsers.currentUser.id)
            .find()
            .then((results) => {
             $w('#input7').value =results.items[0].loginEmail;
 
            })

that’s my code, now the input fields display the user email. I think the “setFieldValue” part is missing. I check the page but don’t understand how the add that line to the rest of the code. I try with

$w("#dataset4").setFieldValue("title","New Title");

but doesn’t work.

It’s working! Thank you so much!!

import wixUsers from 'wix-users';
import wixData from 'wix-data';
wixData.query("Members/PrivateMembersData")
            .eq("_id", wixUsers.currentUser.id)
            .find()
            .then((results) => {
             $w('#input7').value = results.items[0].loginEmail;
             $w("#dataset4").setFieldValue("title", $w('#input7').value);
            })

This is the code, now I want when the submit it’s successful, reload the page.

1 Like