Currently, you can only get email and userID from wixUsers, but it would be nice if you can also get the photo, firstName, lastName and maybe even username! Would be so nice if that were possible.
I actually found a solution to this problem, but itâd be nice to have this feature built-in.
The solution
You can use wixData.query to get data from the dataset Members/PrivateMembersData . This is my script, divided in parts:
Put this all the way at the top of your code:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
Put this underneath an event handler:
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
//script here
})
Change //script here with this code snippet to display the username in a text element:
(I am using a text element with the ID #usernameText1 in this case)
$w('#usernameText1').text = results.items[0].username;
You can fetch any of the data by entering results.items[0]. and then one of these:
loginEmail
_id
name
firstName
lastName
picture
nicknames
slug
language
status
_createdDate
_updatedDate
lastLogin
emails
mainPhone
phones
Man, that is REALLY useful!
I wish I would have seen this post before.
Thanks a lot! Works like a charm
@eriksonsousa Youâre very welcome!
Hi, your answer seems really useful. I get an error though with the â#usernameText1â. Should I be changing this to something else? many thanks
Your elementâs ID should either be set to âusernameText1â, or you should change the â#usernameText1â in my code to your elementâs ID.
The element Iâm talking about must be a text element, in order to show the username correctly.
@webmasterq Hey! Iâm trying to use your example or code but what I need is to extract the ID of the logged user and then pass the personal data (or insert( )) to a different database that corresponds to a form
@andresberegovich Unfortunately, I think you could better post a new forum thread, since I am not really advanced with inserting to databases.
@webmasterq No worries! I already found a solution
Ok
When I use your code, using âusernameâ, it works. But, when I try to get â_createdDateâ, it doesnâtâŚ? Any suggestion for fix?
import wixUsers from âwix-usersâ;
import wixData from âwix-dataâ;
wixData.query(âMembers/PrivateMembersDataâ)
.eq(â_idâ, wixUsers.currentUser.id)
.find()
.then((results) => {
$w(â#textDateâ).text = results.items[0]._createdDate;
})
@michaelallgier I believe thatâs because all the fields with a _ are visible to admin only. There is a bug at the moment though that makes the whole database admin only, so my whole site is kind of broken lol