Hello!
I used this code to get loggedin users name this code worked before but for some reason it stopped working today on both of my sites i need urgent help! i might even lose my job!
here is the code
let user = wixUsers.currentUser;
if (wixUsers.currentUser.loggedIn) {
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w("#CurrentUser").text = results.items[0].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.
@xpuehsan In the meantime try retrieving the name using a backend function. Create a .jsw file on your site, name the file userName and use the following code on it.
//userName.jsw
import wixData from 'wix-data';
let options = {
"suppressAuth": true
};
export function getName(userId) {
return wixData.query("Members/PrivateMembersData")
.eq("_id", userId)
.find(options)
.then( (results) => {
let name = results.items[0].name;
return name;
});
}
Call the function from your page like shown below.
import {getName} from 'backend/userName.jsw';
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
if (wixUsers.currentUser.loggedIn) {
let userId = wixUsers.currentUser.id;
getName(userId)
.then( (name) => {
$w("#CurrentUser").text = String(name);
});
}
});