If the user is already logged on when they visit the page you can simply have a text box element which is linked to the first name of the Wix Members app collection.
As the user will already be logged in that text box will only show the specific users data and so should just display their first name.
If you want to do it through code like you have tried above with the example from Wix Users API.
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; \
} );
Then simply add it to your pages onReady function if they are already logged in when the page loads.
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;
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#firstName').text = results.items[0].firstName;
});
});
Or use onLogin() to do it when they have logged in, although this might need the page to be refreshed for this to work.
https://www.wix.com/corvid/reference/wix-users.html#onLogin
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
});
wixUsers.onLogin( (user) => {
let userId = user.id;
let isLoggedIn = user.loggedIn;
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#firstName').text = results.items[0].firstName;
});
});