Comments section with PrivateMembersData

Hi! I’m building a comments section on a members page only on my site and since the person already had to login to access the page, I would like that the name and the picture fields to be already filled in with their data, and also the comments to have the names and the pictures of the respective members who wrote them. I will be grafeful if someone could help me :wink:

I will explain what I have.
First, I put 3 datasets: Members (dataset1), Read (dataset2, which is read-only), and Write (dataset3, which is write-only). Members is connect to PrivateMembersData. Read and Write are connected to the same database, Comments , which has Picture , Name , Comment and Rating fields.


I did my connections this way: The comment and the rating fields of the form are connected to the the their respective fields on the Write dataset, meanwhile the comment and the rating fields of the repeater (comments section) are connected to the same fields, but on the Read dataset. This works fine, except that the data does not automatically refresh after a comment is sent by hiting the submit button, even after I’ve tried multiple codes.

Then, I connected the picture and the name fields of the form to their respective fields on the Members dataset (PrivateMembersData), which apperently works fine according to the member who is logged in, but I have my doubts if I should have made the connections this way, because when I logged in once with a site administrator account, these fields were assigned to the data of the first registered member of my site, so I guess the connections might be wrong.

And then what completely doesn’t work is the name and the picture fields of the repeater. When I connect them to their respective fields on the Read dataset, they appear completely blank after the comment is sent, what I already expected, since there’s no connection between the Comments database and the PrivateMembersData database (1st picture down below). But when I connect them to their respective fields on the Members dataset, they appear only with the name and the picture of the member currently logged in and not the member who actually commented (2nd pic down below).

I imagine what should be done is to create a way to automatically export the data from PrivateMembersData to the Comments database whenever a new member is created and to find a way to the data identify who commented and which photo is that person, so as to be registered exactly which member did it, but I still haven’t found exactly how to do it, and also I’m new into coding.

A big thanks to anyone that will comment here to help me!

Hello Barbara,

Take a look at my example,

perhaps it can help you to create your won solution…

https://russian-dima.wixsite.com/meinewebsite/comments

Thank you for your reply @russian-dima ! I’ve seen your solution before when I was searching for topics similar to mine, but I don’t think I understand exactly what was done :confused: Could you explain it to me, please?

The most difficulty is to get the right data of the user (for example the User-ID). When you work with front-end, you will just be able to get the ID of the current logged-in-User.

But you will surely need more than just the current-User-Data. You will also need IDs of every user who wants to leave a comment, you may perhaps also want to get a pic of this user from the “PrivateMembersData”.

So in this case you will have to work with the back-end to achieve your aim.

Front-end:

import wixUsers from 'wix-users';
2
3// ...
4
5let user = wixUsers.currentUser;
6
7let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
8let isLoggedIn = user.loggedIn; // true
9
10user.getEmail()
11  .then( (email) => {
12    let userEmail = email;      // "user@something.com"
13  } );
14
15user.getRoles()
16  .then( (roles) => {
17    let firstRole = roles[0];
18    let roleName = firstRole.name;                // "Role Name"
19    let roleDescription = firstRole.description;  // "Role Description"
20  } );
21
22user.getPricingPlans()
23  .then( (pricingPlans) => {
24    let firstPlan = pricingPlans[0];
25    let planName = firstPlan.name;          // "Gold"
26    let startDate = firstPlan.startDate;    // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
27    let expiryDate = firstPlan.expiryDate;  // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
28  } );

And here you can see what you can get, when you use the back-end…

import wixUsersBackend from 'wix-users-backend';
2
3export function getUser(id) {
4  return wixUsersBackend.getUser(id);
5}
6
7/* Returns a promise that resolves to:
8 * 
9 * {  
10 *   "id": "dn8sf9c2-4e9f-a02d-a58d-f244d999729a",
11 *   "memberName": "John Doe",
12 *   "firstName": "John",
13 *   "lastName": "Doe",
14 *   "nickname": "johnd",
15 *   "slug": "johnd123",
16 *   "language": "en",
17 *   "status": "ACTIVE",
18 *   "loginEmail": "john.doe@somedomain.com",
19 *   "creationDate": "2019-08-05T11:29:39Z",
20 *   "lastUpdateDate": "2019-08-12T12:29:43.810Z",
21 *   "lastLoginDate": "2019-08-12T13:42:30Z",
22 *   "emails": [
23 *     "john.doe@somedomain.com",
24 *     "doughyjohn@anotherdomain.com"
25 *   ],
26 *   "phones": [
27 *     "5555555555",
28 *     "5555555556"
29 *   ],
30 *   "labels": [
31 *     "contacts-new",
32 *     "contacts-site_members_approved"
33 *   ],
34 *   "picture": {
35 *     "url": "https://.../photo.jpg"
36 *   }
37 *   "customText": "Custom Text",
38 *   "customNumber": 12345
39 * }
40 */

With this code, you can search a user by ID in the “Private-MembersData”

You can test, my example and write a comment. You will see that you have first to press the green-button.

This example-version is not a perfect one, but it works anyway.
I used a table + RichTextField to generate this commentbox.

A much more advanced version you will find on my site in the footer-section.

got it! thank you!
this worked fine, but now I wonder how can I properly do my data connections and which code command to write to trigger the user data hitting the button (like your green button)

@barbarabessals2 , you can just use:

$w.onReady(function () {

}

@arthurvalatin I did it, thank you! But don’t I need to write nothing after to get the user’s info? And connect the data as well?

we use this script on site .