Hello all,
i am having a problem in comparing IDs from my userProfile database.
Each user is stored with its own id, let’s say…
my ID is ca97c31f-8f52-456e-bc2e-482fe06c690d
User A is ID c9d99e9b-3694-4db8-9c01-0827e9571255
Every user profile is loaded in a dynamic page that contains a button ( Edit Profile )
What i am trying to do is : if I visit my own profile page (based on the ID), the button is enabled, while if i visit User A profile page the button is disabled.
I was thinking to use a comparison between my user ID (the visitor) and the User A ID (the visited profile), something like:
if my ID = userA ID then
$w(“#button2”).show() //the button appears only if i visit my own profile (IDs are equal)
else
$w(“#button2”).hide() //the button disappears if i visit another user’s profile (IDs are not equal)
but honestly don’t know how to gather the IDs and set the if cicle…
Anyone can help?
First of all get the current item (userId) from the dynamic dataset.
Then, get the current user Id.
Compare using if-condition.
#dynamicDataset - Dynamic dataset of the Dynamic Page
#buttton2 - Button
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;
let id = $w('#dynamicDataset').getCurrentItem().userId;
if (userId === id) {
$w('#button2').show();
}
else {
$w('#button2').hide();
}
});
Hi Ajit, thank you for you help, unluckily it does not work.
The:
let id = $w('#dynamicDataset').getCurrentItem().userId;
gave me ‘undefined’, so i have changed it in:
let id = $w('#dynamicDataset').getCurrentItem()._id;
yes it seems to compare the owner ID with the ID of the visited page, but the ‘my ID’ i am using is not the owner ID.
I’ll explain showing you the content of the database:
Here are the users:
The wixUsers.currentUser etc. points me to the owner ID, my wix user ID as a proprietary of the website, while the $w(“#dynamicDataset”) etc correctly points to my social network user id.
As you can also see, more test users have the same ‘owner’ (wix ID) but different social network id, because i have created their profiles in the database with my own wix account.
The result is that when i visit my own page (record 4 in the database), the 2 IDs don’t match and the button is not shown.
My routine should compare the content of the cell ID for all users, so that in my case, when i visit my own profile page, the two IDs match.
Any idea?
Hi Alessandro Demontis
Is this what you are trying to do ? →
You have a ‘userProfile’ database.
The owner field of the database can’t be used because, you created the fields.
The _id field, is the field, which stores the userId of the users.
You have a dynamic page.
In the dynamic page, there is a button.
If the current user’s id matches that of the dynamic page item (_id) then button should show …
If this is, then →
dynamicDataset - dataset of the dynamic page.
button2 - button to show/hide.
import wixUsers from'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;
let itemUser = $w('#dynamicDataset').getCurrentItem()._id;
if(userId === itemUser) {
$w('#button2').show():
}
else {
$w('#button2').hide():
}
});
One more ,
Wixusers.currentUser does not return the _owner field of the database, but returns the userId of the person who is viewing your site right now…
currentUser
Gets the current user viewing the site.
Gets a User object containing information about the user currently viewing the site.
For reference - > https://www.wix.com/corvid/reference/wix-users/introduction
Hi Ajit, thank you sadly it does not work. Anyway, I have solved passing my own ID via a session.setItem / getItem from the login page to the user profile page, and now it works.
@ademontis
Is this what you are trying to do ? →
You have a ‘userProfile’ database.
The owner field of the database can’t be used because, you created the fields.
The _id field, is the field, which stores the userId of the users.
You have a dynamic page.
In the dynamic page, there is a button.
If the current user’s id matches that of the dynamic page item (_id) then button should show …
If this is, then →
dynamicDataset - dataset of the dynamic page.
button2 - button to show/hide.
import wixUsers from'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;
let itemUser = $w('#dynamicDataset').getCurrentItem()._id;
if(userId === itemUser) {
$w('#button2').show():
}
else {
$w('#button2').hide():
}
});
One more ,
Wixusers.currentUser does not return the _owner field of the database, but returns the userId of the person who is viewing your site right now…
currentUser
Gets the current user viewing the site.
Gets a User object containing information about the user currently viewing the site.
For reference - > https://www.wix.com/corvid/reference/wix-users/introduction