Display username on the website after user has logged in

Hello, I’m trying to figure out how I can use the currentUser.Id to do a database search for the username and then display it in a text element.
So far I’ve figured out how to do a search

if (wixUsers.currentUser.loggedIn){
//Get current user’s data using their Id
wixData.get(“users”, wixUsers.currentUser.id)
.then((result) => {
//Use data to display user’s name
$w(“#text34”).text = result;
//I don’t know the syntax that will allow me to display just the username
})
. catch ((err) => {
let errorMsg = err;
});

But I don’t know the syntax that’ll allow me to display just the username, not the entire row of data (if that’s what is returned from that search function).
Thanks in advance!

Hi Shiro,

if you have a private members collection present in your site like this:

You can easily use the following code:

wixData.query(“Members/PrivateMembersData”)
.eq(“_id”,user)
.find()
.then( (results) => {

if (results.items.length > 0)
{
let details = results.items[0];
data.name=details.name;
data.email=details.loginEmail;
data.phone=details.mainPhone;
}

else
{
}

} ) 
. catch ( (err) => { 

let errorMsg = err;
} );

However, you need to create a web module in the Backend section and write this code inside a function there. Because from front-end code won’t have permissions to access this database.

Also, if you don’t have that PrivateMembersData collection, you can easily add it by adding the E-commerce app to your site, and then deleting that E-commerce app if you don’t need it. I think that should work.

Cheers,
Sameer

I assume I would have to create an array called “data” so I can use

let details = results.items[0];
data.name=details.name ;
data.email=details.loginEmail;
data.phone=details.mainPhone;

right?

Did you use Member’s area?

I didn’t use member’s area because I can’t create custom fields in the register form that the user can enter that will then be displayed in the member’s area.

The website I’m creating for the user is to register for a game they’ll be playing on the day. The register form needs to have their personal details (name, phone, email, etc) as well as their team details (eg. team name, number of people, type, etc). I don’t think I can create those fields in the pre-made register form, so then the member’s area will have those fields displayed.

So do I need to create an array for that or no?

HI

i didn’t use a backend function as such i created a hook to join first and last name.

HOOK

export function MemberProfile5_afterQuery(item, context) {
item.fullName = item.firstName + " " + item.lastName;
return item;
}

and then on my page code accessed the current item full name field and put in the text element as follows

PAGE CODE

function populateCalculatedFields() {
const currentItem = $w(“#MemberProfile5”).getCurrentItem();
$w(“#fullName”).text = currentItem.fullName;
}

Hello,
I have a form created from entry fields within the private members area and connected to a database and I need to show 2 results in a visible area. A table with a name column of the member and the other with the points that you add through that form. I need help for the creation of that ranking table.