How to get customer's first name for triggered email variable?

I’ve tried this code:

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

but for some reason it shows red mark next to firstName stating that it is not defined.

There is a good example in the Wix Users API already.
https://www.wix.com/corvid/reference/wix-users.html

How can I get the current user’s name?
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; \
  } );

So to get the above to work, you would need to add the code from the current user section as shown here.
https://www.wix.com/corvid/reference/wix-users.html#currentUser

Then you would need to do something to make the query work, like click on a simple button for example.

Also, then change the lastName to the firstName, so that it matches the field in the Wix Members app.

You can see the Wix Members app collection here.

Thank you for fast response GOS! I do have current user ID defined by this:

[const](const userID = wixUsers.currentUser.id;) [ userID = wixUsers.currentUser.id;](const userID = wixUsers.currentUser.id;)

so my code looks like this

if (myPurchase.wixPayStatus === “Successful”) {
wixData.query(“Members/PrivateMembersData”).eq(“_id”, wixUsers.currentUser.id).find().then( (results) => {
firstName = results.items[0].firstName;
} );
wixUsers.emailUser(‘emailID’, userID, {
variables: {
“paidplan.title”: wixUsers.currentUser.PricingPlan.name,
“subscriberName” : userID
}
})
.then(() => {
console.log(“Triggered email sent”); // email has been sent
})
. catch ((err) => {
console.log(err); // there was an error sending the email
});

“paidplan.title” and “subscriberName”
are variables in the email.

I still get red dot stating that firstName is not defined

I had done similar in a recent post as shown here.
https://www.wix.com/corvid/forum/community-discussion/put-value-in-database?

Test site.
https://givemeawhisky.wixsite.com/inserttest

Code Used.

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

$w.onReady(function () {
});

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

export function button1_click() {
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#input1').value = results.items[0].firstName;
$w('#text22').text = results.items[0].firstName;

let toInsert = {
firstName: $w("#input1").value
};

wixData.insert("mannequinpreferences", toInsert)
.then( (results) => {
$w('#dataset1').refresh();
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
})
}

If you want to have the users first name in your triggered email, then you need to have it in the triggered email variables.

See here.
https://support.wix.com/en/article/corvid-tutorial-sending-a-triggered-email-to-members

I’ve seen that tutorial - it only shows how to get first name from the input field. Unfortunately I don’t have input field. Once customer purchases plan - PurchasePlan() function invokes

If you want to get the user id, then use it as shown here.
https://www.wix.com/corvid/reference/wix-users.User.html#id

let userId = user.id;  // "r5cme-6fem-485j-djre-4844c49"

this is what I get when I put this query in variables section

When I remove ; firstName shows as not defined.

Can you post your full code so I can see it.

As you are using Wix Users and the emailUser function, then why not just try something like this.
https://www.wix.com/corvid/reference/wix-users.html#emailUser

import wixUsers from 'wix-users';

// ...rest of code...//

let userId = wixUsers.currentUser.id;
let value1 = wixUsers.currentUsers.firstName;

wixUsers.emailUser("emailID", userId, {
    "variables": {
      "variable1": value1,

Or add it like this to your current code.

wixUsers.emailUser('emailID', userId, {
variables: {
"paidplan.title": wixUsers.currentUser.PricingPlan.name,
"subscriberName" : wixUsers.currentUsers.firstName

Let me try that!

By the way with your code above…

const userID = wixUsers.currentUser.id;
"subscriberName" : userID

That would return the actual user id which is what is shown here.

let userId = user.id;  // "r5cme-6fem-485j-djre-4844c49"
// The bold type here is the user id.

I’ve tried bunch of options but Neither “paid plan.title” (plan name) or “subscribersName” (customer’s first name) are not showing correct information.

Here is my code:

import wixPaidPlans from ‘wix-paid-plans’;
import wixPay from ‘wix-paid-plans’;
import wixData from ‘wix-data’;
import wixCrm from ‘wix-crm-backend’;
import wixUsers from ‘wix-users’;

export function button1_click() {

wixUsers.emailUser('RvW96RK', userID, { 
						variables: { 
							
								"paidplan.title" : wixUsers.currentUser.getPricingPlans() 
						                   	.then( (pricingPlans)  => {  
								                let firstPlan = pricingPlans[0]; 
								                let planName = firstPlan.name; 
							       }) , 
								
								 
								"subscriberName" : 	wixUsers.currentUser.firstName 
							
							} 
					}) 
					.then(() => { 
						console.log("Triggered email sent"); // email has been sent 
					}) 
					.catch((err) => { 
						console.log(err); // there was an error sending the email 
					}); 

}

and my test function for first name is not working correctly either. What am I missing?

Figured that one out:

I ended up creating a text and sending result to that text object:

wixData.query(“Members/PrivateMembersData”)
.eq(“_id”, wixUsers.currentUser.id)
.find()
.then((results) => {
//This changes the text to for example Hi John:
$w(“#text60”).text = results.items[0].firstName;
});

After that in variables I’ve referenced that text object

variables: {

“subscriberName”: $w(“#text60”).text

}

Great, well done :+1: