Can anyone share me the code to get current user name and put it into a specific database's title?

Thanks you in advance.
Best Regards
Ryan

The below code will get you the email for a logged in user. What do you mean by put it into a specif database title ?

import wixUsers from ‘wix-users’;

$w.onReady( function () {

if (wixUsers.currentUser.loggedIn) {

let user = wixUsers.currentUser;

    console.log(user); 

    user.getEmail() 

        .then((email) => { 

let userEmail = email;
console.log(userEmail);
})
}
})

I have a similar need than Ryan. I think he needs to store those values on a database called “X” at every time anyone has logged in.

I need this in order to compare if anyone else has been logged in with the same account but different ip. I can read ip and username but I am not able yet to store in a database.

pass the current user email onto a collection/database like so…

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

$w.onReady( function () {

if (wixUsers.currentUser.loggedIn) {

let user = wixUsers.currentUser;

    console.log(user); 

    user.getEmail() 

        .then((email) => { 

let userEmail = email;
console.log(userEmail);

//submit user email to collection

let toInsert = {
“email”: (userEmail) //email is the collection field id , change as necessary

            }; 

            wixData.insert("collectionName", toInsert); //change the collection name as necessary 

        }) 

} 

})

1 Like

Hi Mike, thanks for the code, this looks like exactly what I need, will try it when I get home later.

I have a question, what triggers the event? Is it OnReady ? When the page is done loading the email will be submitted ?

In my case I need it to submit only when a submit button is clicked. What kind of code should I add for that ?

Sorry if I’m asking too much and needing to be spoon-fed, total newbie here, trust me you just saved me a few days time with the code above.

@mikemoynihan99 Thanks for your help. I have it done but when I enter the web like a user and I check in the live collection the things I collect doesn’t show up, whereas when I try the Preview in the editor things show up in the sandbox. Any ideas why I am wrong?

Thanks for your help!!!

@frandullera Did you remember to sync the sandbox data to the live collection

@thecandymachinefreel Yeah I did that as well. It just doesn’t show up.

@frandullera
check that you have the collection permissions set correctly, i.e. that you have permission to write to the collection

@Ryan
if you want to execute the code when a button is pressed just add an onClick after the onReady…

$w(‘#submitButton1’).onClick((event) => {

//insert code here for what needs to happen when the button is pressed

//end of onClick event
})

@mikemoynihan99 Hi Mike, I used your code and it works like a charm ! Thank you so much for sharing. I have another question, I have a gallery that is linked to a dataset, inside the dataset there’s the Email field and a few Image Fields, I only want to show those images for that specific user who is logged in with the same email. (that means user only see images from the same data row with their matching email.) because the images will be uploaded by the admin and not the user, I don’t think I can use the Owner Filter

how can I do that ? Thank you so much in advance