[Solved] insert current user's mail to a dataset after they click a button

Wondering if I can know which member clicked a button (e.g. applying an event).

1- A member logged in
2- Member clicked a confirm button in lightbox (e.g You are applying this event, confirm?)
3-Save member’s mail address in to a dataset.

I know if I used a form, I can get the e-mail notification. However, I don’t want the members fill in anythng.

This is my code:

import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
let user = wixUsers.currentUser;

export function button2_onclick() {
user.getEmail()
.then((email) => {
let cand_mail = email;
});

}

There is blank data saved in the dataset.

Thank you so much!

I have changed to the following, but stil fail…

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

let user = wixUsers.currentUser;
let email = user.getEmail();

export function button2_onclick() {

wixData.query("job_apply_history") 

    .eq("cand_mail", email) 

    .find() 

}

This is also not working :frowning:
I am really new to coding and trying my best, please kindly help!


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

export function button2_onclick() {
let user = wixUsers.currentUser;
let userEmail;

user.getEmail() 
    .then((email) => { 
        userEmail = email; 
    }); 

const toInsert = {
“cand_mail”: userEmail
};
// add the item to the collection
wixData.insert(“job_apply_history”, toInsert)
. catch ((err) => {
console.log(err);
});

}

@kana The simple answer is if the user is not logged into your site and you do not ask for the information in a form or by giving them a chance to register on your site then you cannot get the email address.

If they are logged in then you should have access to their email address. Your code isn’t far off what you need BUT you need to make sure that you are using the correct data collection field keys in your insert statement. Here is a proposal for what you need to do modifying your code as necessary:

import wixData from 'wix-data'; 
import wixUsers from 'wix-users';  

export function button2_onclick() {
    let user = wixUsers.currentUser;

    // If we are logged in then save the email address
    if (user.loggedIn) {
        // User is logged in so lets save their email address.
        let userEmail;
        user.getEmail()
        .then((email) => {
            userEmail = email;
//------------------------------------------------------//
//    We need to process the email information inside of the then() function call 
//    Because the getEmail call will take time and we don't want
//    to try to use the userEmail value until we know we have a value
//    to work with
//------------------------------------------------------//
            const toInsert = { "cand_mail": userEmail }; 
            // add the item to the collection
            // We return the result of the insert. This way if the getEmail function fails 
            //OR the insert fails then the catch() will report the error.
            return wixData.insert("job_apply_history", toInsert) ;
        })
        .catch((err) => {
            console.log(err);
        });
    }
}

NOTE: The wixData insert is used INSIDE of the then following the getEmail call. Also check that the cand_mail property name and job_apply_history are correct.

Hope this helps you out.

Cheers
Steve

@stcroppe Steve Thank you so much for replying!!!
Blank row is inserted after I use the code above, so I guess is not the code but something else is wrong. :frowning:
This is what I got:

What I have tried:
-connect / remove the button to the dataset but still the same. (actually am I suppose to connect them?!)
-I have checked that “everyone” have right to change the collection
-insert a default value after user pressed the button, still only blank row appear↓
const toInsert = { “cand_mail”: “1235” };
return wixData.insert(“Job_apply_history”, toInsert) ;
})
. catch ((err) => {
console.log(err);
});

Anything else I may have done wrong?? Please kindly let me know. Thank you so much!!!

Kana

@kana You have the wrong data collection name. You should change the uppercase J to a lowercase j in Job_…

Cheers

@stevesoftwareservice Thank you Steven! I have changed back to lowercase j but still the same…
When I press the button, a blank row is created but it is a blank row (not even display a error message). So i think the button is able to link to the collection but not inserting the value.
Any idea? Thank you so much!!

FYI: the “type” for cand_mail is “text”, should I try other?

KANA

What is being printed out in the console when you preview the page? Can you share the published url for page you are trying to get working?

Dear Steven

Here is the link:
https://nachrasia.wixsite.com/kingsway-hk/jobinfo/KA1111

After you press the “Apply” button, there should be a lightbos ask you to log-in. Please use the following accout:
ID: test@123.com
password: test123

After log-in, the “are you sure??” lightbox will apply and the user’s mail should be insert to collection(name changed to job_applyhist, and property name is “candEmail”) after pressing confitm button, which I failed to do now.

Thank you so much for your help!

KANA

@kana Hi there:

I have had a look at your page and it seems that you are missing a binding to your onclick() handler. Here is a screenshot:


Notice that the text below the Confirm button says “Your content has been submitted”. In the Developer screen to the right you will see that I have set breakpoints in the button2_onclick() function on line 5. My assumption is that the button2_onclick() function should be connected to the Confirm button element using the property panel for the element. My guess is that it is not connected. BUT I suspect that the button has been connected, using data binding, to a dataset otherwise the “Your content has been submitted” message probably wouldn’t have been displayed.

I think you are getting datasets and the data collection API wix-data mixed up? Since I cannot see the page in the editor (unless you add me) this is the best I can do.

If you do not change anything else in the editor and simply change the code then you could try doing this:

import wixData from 'wix-data'; 
import wixUsers from 'wix-users';  

$w.onReady(() => {
    // Initialise the button2 click handler
    $w('#button2').onClick(button2_onclick); // Don't do this AND set the handler in the property panel. One or the other! This is easier to debug ;-)
});

export function button2_onclick() {
    let user = wixUsers.currentUser;

    // If we are logged in then save the email address
    if (user.loggedIn) {
        // User is logged in so lets save their email address.
        let userEmail;
        user.getEmail()
        .then((email) => {
            userEmail = email;
            //------------------------------------------------------//
            //    We need to process the email information inside of the then() 
            //    function call 
            //    Because the getEmail call will take time and we don't want
            //    to try to use the userEmail value until we know we have a value
            //    to work with
            //------------------------------------------------------//
            const toInsert = { "candEmail": userEmail }; 
            // add the item to the collection...
            // We return the result of the insert. This way if the getEmail function 
            // fails OR the insert fails then the catch() will report the error.
            return wixData.insert("job_applyhist", toInsert) ;
        })
        .catch((err) => {
            console.log(err);
        });
    }
}

Cheers
Steve

1 Like

@stcroppe Steve Thank you so much it works! There is still a lot to learn for me, thank you so much for being patient and friendly. I really appreciate your help. :slight_smile:

KANA

1 Like

could i use this code to put the users email into a dataset, but then use wixLocation.to(“/members/” + userEmail); in order to send the user to their unique page using a dynamic page with an email field?

@[Patrick Westerkamp] this should be doable. The way to check the location string is look at the data collection which is driving the dynamic page. It will have the specific links you need to create in a special column added by the Corvid backend.