Hi there, I created a space for members to sign up, view their profiles, etc through help of a code listed on WIX (Login/My Profile)
Functionality works perfect and everything is fine until I discovered an error by chance.
So I login with the credentials i signed up with, and there I am on homepage, I click on the My Profile button and it takes me to my dynamic page, which is exactly what I want.
However, I noticed when I am on any other page besides home page, clicking on My Profile does not do anything nor does it take me to my Profile Dynamic page.
I have my code in the Site area of the code. This code also shows in the Page area of the code when I am on the Home page & only the home page. Could that be the reason why it's happening? I tried removing the code from the Page area of the home page and leave it only on the Site area but then the functionality stops working. So what am I doing wrong? Would appreciate any help !
Here is my code:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#button3").label = "LOGOUT";
$w("#button4").show();
}
else {
$w("#button3").label = "LOGIN/SIGNUP";
$w("#button4").hide();
}
} );
export function button3_onclick() {
if(wixUsers.currentUser.loggedIn) {
wixUsers.logout()
.then( () => {
$w("#button3").label = "LOGIN/SIGNUP";
$w("#button4").hide();
} );
}
else {
let userId;
let userEmail;
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
userEmail = email;
return wixData.query("Profile")
.eq("_id", userId)
.find();
} )
.then( (results) => {
if (results.items.length === 0) {
const toInsert = {
"_id": userId,
"email": userEmail
};
wixData.insert("Profile", toInsert)
.catch( (err) => {
console.log(err);
} );
}
$w("#button3").label = "LOGOUT";
$w("#button4").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button4_onclick() {
wixLocation.to(`/Profile/Members/${wixUsers.currentUser.id}`);
}
hello mohabsaleh,
Have you figured it out yet? If not, i would love to help or point you to someone who is a expert at wix code and wix databases. Have you heard of fiverr.com? this guy is amazing and can do anything that you would think is impossible. His direct link is fiverr.com/tiaanrich
If you message him, be sure to let him know that Austin Searle sent you. I am positive that what you are describing with the url part or anything related to your members section of your site, he will be able to help.
I'm afraid getting the name of the current user using the wix-users API is not yet supported.
You can use ID or Email, as you can see here: https://www.wix.com/code/reference/wix-users.User.html
I did that.
I changed the URL from Profile/Members/{ID} to Profile/Members/{NAME}
and on the code block of the Site, i changed the last part of the code:
From:
To:
But that did not do the trick, it takes me to an error page : /Profile/Members/undefined
Any suggestions?
You can change the url pattern of a dynamic page in the Page Info setting panel.
Also, see: https://support.wix.com/en/article/creating-dynamic-page-urls
I have a final question if I may Itai,
When a member clicks on My Profile, the link of the navigation shows member ID.
How can I manipulate the code to make it show Name instead?
For example:
This is what it shows:
/Profile/Members/e9adfe10-69bf-44ca-9b2b-c72fad4d3e91
Instead, I want it to show:
/Profile/Members/NameOfMember
Thank you so much for your help and time !
Yes thank you that did the trick !!!
Apparently the problem was in the name of #dataBase.
They were different, and now it worked.
Hey again,
I can see that the Page code and Site code are very similar, but not completely identical! Therefore one works and one doesn't..
Try copy pasting the Page code into Site code, and removing it from Page code. It should work.
Good Luck,
Itai
Hello Itai,
Thank you for your reply; yes the components are in fact located in the header, and the code is indeed in the Site code !
Another thing I noticed Itai, is that when I sign up from say any page except the home page, the Login/Signup button does not disappear even if I am logged in. Again to solve this I have to go to home page or any other page and then the button hides, and everything is back to normal.
I appreciate it if you can take a look at it.
My website:
https://xchallenger.wixsite.com/xchallenger/
Hey,
It's definitely sounds related to Site vs Page code.. In order for something to work in all pages, the relevant components should be in the header / footer, and the code should all be in the Site code.
Can you provide a link to your site so I'll be able to check out your configuration?
Thanks,
Itai