How Do You Retrieve A Site Member ID

I have found thru all the online examples and testing with examples given that wix-crm will only send email to a member that is logged in. This is NOT what I want to do. I want to send email to other admins of the site whose email I already know. According to documented API I need to use wix-users-backend. Reference the code shown in the API example:

import wixUsers from ‘wix-users-backend’;
export function myBackendFunction() {
let userId = // get user ID <<<<<<<<<HOW DO YOU GET THE USERID ???
let value1 = // value for variable1

wixUsers.emailUser(“emailID”, userId, {
“variables”: {
“variable1”: value1,
“variable2”: “value for variable2”
}
} )
.then( () => {
// email has been sent
} )
.catch( (err) => {
// there was an error sending the email
} );
}

the API example for getUser shows the following that would retrieve values, but this is of NO USE TO ME since it assumes I want to get a site member who’s id matches a given id!

import wixUsers from ‘wix-users-backend’;

export function getUser(id) {
return wixUsers.getUser(id);
}

/* Returns a promise that resolves to:
*

  • {
  • “id”: “dn8sf9c2-4e9f-a02d-a58d-f244d999729a”,
  • “memberName”: “John Doe”,
  • “firstName”: “John”,
  • “lastName”: “Doe”,
  • “nickname”: “johnd”,
  • “slug”: “johnd123”,
  • “language”: “en”,
  • “status”: “ACTIVE”,
  • “loginEmail”: “john.doe@somedomain.com”,

WHAT STUMPS ME IS HOW DO YOU GET THE USER ID WHEN YOU DO NOT KNOW IT? I want to send email to other site admins whose EMAIL I do know…

Note that you mention both Wix CRM and Wix Users, so note that they are two different Api.

What’s the difference between wix-crm and wix-users?
The CRM API contains functionality for working with your site’s contacts. The Users API contains functionality for working with users who are logged-in members. Note that all members are also contacts, but contacts are not necessarily members.

As for getting the site members userid, you are getting it in the above info, it is the id value that you simply use.

  • “id”: “dn8sf9c2-4e9f-a02d-a58d-f244d999729a”,

Otherwise, if you have members dataset, then you can just go to your dataset and get it from the id field and simply paste it into the Wix Users Backend example to send to a specific site member and not the logged in site member.

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

https://www.wix.com/corvid/reference/wix-users-backend.html#emailUser

You wrote: Otherwise, if you have members dataset, then you can just go to your dataset and get it from the id field and simply paste it into the Wix Users Backend example to send to a specific site member and not the logged in site member.

I triied that, it DOES NOT WORK!

Code:

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

$w.onReady( function () {
})

export function messageSendButton_click(event) {
let userId = “834e16d4-c47e-4015-a0c8-c6f4ed6d0739”
wixUsers.emailUser(“RgEOdam”, userId, {
“variables”: {
name: “Test User”,
email: “2jdsmusic@gmail.com”,
state: “FL”,
message: “Just a Test, reply to above email if you received it”
}
} )
.then( () => {
console.log(“// email has been sent”);
} )
. catch ( (err) => {
console.log(“// there was an error sending the email”);
} );
}

You are trying to call wixUsers (of wix-users-backend) from front end (page) code. You can’t do that, not directly.

https://support.wix.com/en/article/corvid-web-modules-calling-server-side-code-from-the-front-end

@cwvega76

As Lee has already stated below, you need to add the email user to a site member in your backend and not the fronted.

If you simply add it into your page code, then you are restricting yourself to emailing the logged in site member only, hence why you need to use it in the BACKEND so that you can email a specific site member.

Read the Wix Users Backend API Reference, it is clearly stated in there.
https://www.wix.com/corvid/reference/wix-users-backend.html#emailUser

@givemeawhisky
I have read and re-read that documentation and damned if I see where it explains this? Are you saying that the site members info that is generated when a site visitor does a SIGN UP and gets a wix Login account, that this is NOT what it uses? Is there a pseudo database in the “backend” that contains the site members info? If so, how it is added to the backend? Or would I just hardcode some IDs?

About 3 months ago I hired a wix consultant write me the backend but it doesn’t work. The example above was me just trying a simplified approach in order to try an grasp how the heck this works.

I have uploaded the backend code. When called from my membership update / renewal form it e ONLY sends email to me (website admin) and the member doing the renewal. All the other email addresses produce the followin error:

Renewal Successful wixcode-worker.js:18:102534
Wix code SDK error: The userId parameter that is passed to the emailUser method cannot be set to the value . It must be of type uuid.

Here is the backend code:

Here is the page code:

@cwvega76

I have uploaded the backend code. When called from my membership update / renewal form it e ONLY sends email to me (website admin) and the member doing the renewal…

That is because your triggered emails are still being sent through your frontend code (renewal).

//rest of code//
//Send Triggered emails

export function sendTrigEmail() {

	let emailBuilder = [presidentID, vpMembFinanceID, webmasterID, RegDirID];

	dateString = endDate.toLocaleDateString("en-US");

	//sends the clients Email
	wixUsers.emailUser('RBmfp1o', user_id, {
		variables: {
			firstname: Fname,
			expiryDate: dateString,
			totalAmount: payString,
			payType: PaymentMethod,
			membershipStatus: memberStatus
		}
	})
}

	// get member IDs
	
	//email the webmaster
	wixUsers.emailUser('RBmeAv4', webmasterID, {
			variables: {
//rest of code//

This means that as both myself and Lee have already previously stated, that the only people who will get the triggered email is yourself as website admin and the current logged in user who has submitted the form.

For it to work, you need the triggered email sending part to be through your backend code

If you look at the Wix Users API Reference, you will see that there are two sections for Wix Users:

If you read the Wix Users section you can see the info about the difference between the two versions as shown below.

How do I send an email to a user who is not the currently logged-in user?
Using the emailUser() function of wix-users you can only send emails to the currently logged-in user. However, from the backend, using the emailUser() function of wix-users-backend you can send emails to any site member.

Plus, also note that the Wix Users API is only fully functional when you are testing it through a fully published website, it should not be used through the preview mode for any testing.

Note

  • 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.

  • The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

Why is the wix-users API not functioning properly when I test my code in preview?
The wix-users API is only partially functional when previewing your site. You need to view a published version of your site to see its full functionality.

With regards to your backend code, you have put all your code through the data.fs file, however this should be for your Wix Datahooks and not your full code.
https://www.wix.com/corvid/reference/wix-data.Hooks.html
https://support.wix.com/en/article/corvid-using-data-hooks

Your backend code should be in a jsw file as Lee stated in his link below, so in your frontend code you call the backend module which does all the work for you with regards to the triggered email.
https://support.wix.com/en/article/corvid-web-modules-calling-server-side-code-from-the-front-end

Also, in the Wix Users info, it shows code for searching for the current users name.
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; \
  } );

Now yes I know that this currently gets the logged in users last name through the userid, however like you have done on your backend file with your search for the site members email address, you can modify it to suit your needs to get the userid of the site member that you are requiring.

Plus, as you are using the Wix Members app and the collection from that app that gets added automatically when you use it, then you are correct in having to use the Wix Data Options to suppress checks and hooks etc.

As you are using the Wix Members app own Members/PrivateMembersData collection, then you will already have the userid of the required site member in that collection itself.

See here for the Wix Member app Members/PrivateMembersData collection fields.

If you can not see the id field in your own Wix Members collection, then you need to unhide it from the hidden fields.

With regards to your error about value and must be of type uuid…
Please note that the id field, which is the userid field that you need to refer to for the userid, is of text type string.

Once the site member is signed up with your website, they will be given a unique uuid which will be unique to only that member and will be used as their id whenever they submit data to another dataset for example, it will never change once generated.

Therefore you need to making sure in your code that the value passed in your code is of a text type string, as stated in the id field notes for the Wix Members app collection
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields#id-_id

ID (_id)

Description : The member ID that was created by the server. This is a system field and is hidden by default.
Type : Text
Can connect to data : Yes
Can use in dynamic page URL : Yes
Read-only : Yes

And also in the info for when you create your own dataset through Wix too.
https://support.wix.com/en/article/about-your-database-collection-fields#system-fields
Field Name - ID
Field Key - _id
Field Type - Text
A unique identifier for the item. You can assign the ID a value when you import new data from a CSV file. Otherwise the ID is a random UUID.
Using Corvid by Wix you can also assign the ID a value when adding items with the Data API.
Once defined the ID cannot be edited.

It is exactly the same for the email field of the Wix Members app collection too.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields#login-email-loginemail480

Login Email (loginEmail)

Description : The email address the member uses to log in to your site. This is the address they supplied when they signed up. This is the Main field.
Type : Text
Can connect to data : Yes
Can use in dynamic page URL : Yes
Read-only : Yes

It might just be a simple case of the function expecting a string but getting a numerical value instead, therefore you need to make sure through code that the value is of a string type.

Finally, for the Wix Users Backend part of it which you can have a read of here.
https://www.wix.com/corvid/reference/wix-users-backend.html

When you go to the emailUser function in the Wix Users Backend section.
https://www.wix.com/corvid/reference/wix-users-backend.html#emailUser
You will see there how to do it through the backend code as it gives you a full description along with code samples.

emailUser( )
Sends a Triggered Email to the specified site member.

Description
To learn more about Triggered Emails, see:

Before using the emailUser() function, you need to set up at least one Triggered Email.

Specify which email to send by passing the email’s ID in the emailId parameter.

Specify which member the email is sent to by passing the member’s user ID in the toUser parameter.

If the specified Triggered Email contains variables , you can pass values for those variables using the optional options parameter. You pass a TriggeredEmailOptions object which contains the values to be inserted into your email in place of the variables defined in that email. The values passed must be strings. If the object you pass to the options parameter does not contain a key:value pair for a variable in your Triggered Email, the fallback value defined when creating your Triggered Email is inserted in place of the variable.

Note that Triggered Emails generates a code snippet for each of your email templates. The generated code includes the email’s ID and keys for all the email’s variable names. You can copy and paste the snippet into your code. Then, you need to define values for the toUser property and for each variable key. To learn how to use the generated snippet in your code, see How to Send a Triggered Email with Code .

Send a Triggered Email to a site member

import wixUsers from 'wix-users-backend';

export function myBackendFunction() {
let userId = // get user ID

wixUsers.emailUser("emailID", userId)
.then( () => {
// email has been sent
} )
.catch( (err) => {
// there was an error sending the email
} );
}

Send a Triggered Email to a site member with variable values

import wixUsers from 'wix-users-backend';

export function myBackendFunction() {
  let userId = // get user ID
  let value1 = // value for variable1

  wixUsers.emailUser("emailID", userId, {
      "variables": {
        "variable1": value1,
        "variable2": "value for variable2"
      }
    } )
    .then( () => {
      // email has been sent
    } )
    .catch( (err) => {
      // there was an error sending the email
    } );
}

All your triggered email code must be through your backend file and not through your frontend code, so that Wix Users Backend can run and send the email to a specific user. Otherwise it will be running as Wix Users through your frontend and you will only be able to access the currently logged in site member.

Also note.
The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

Thanks for the detailed clarifications.

  1. I did not upload the jsw portion of the backend code written by the wix consultant. I will upload it for review.

  2. I AM aware that this has to be tested in the published site.

  3. I had obtained the database “_id” field from the 6 club officers that I am trying to send emails to and stored them in a “CVOA-Officers” dataaset. the code was written to retrieve each one of them to use in the emailUser function.

  4. I just checked the CVOA-Officers database… the field which I called wixID is a copy of the _id in the custom membership database, both of which are defined as TEXT fields.

I think I have all the pieces but it’s just not glued together properly.

Following file is the email.jsw from the backend. I believe it still may have old functions
in there from when we were using sendgrid.

Thanks Again.
JD