I am attempting to link the profile button on my login page to a dynamic page. I thought it would be easy and I have seen plenty of examples, however my button keeps going to a static page. Next, I tried using code from this page https://support.wix.com/en/article/wix-code-tutorial-building-your-own-members-area
this line in particular does not work, it just goes to a jumbled mess of numbers
wixLocation.to(`/dmList/${wixUsers.currentUser.id}`);
I would like to do a query into my database and select the current user's Username (a custom field) and use that with the wixLocation.to() function. How in the heckin heck do I do that?
Oh and by the way, that jumbled mess of numbers you are referring to in your post, that is probably the user id you are seeing.
user.id // "r5cme-6fem-485j-djre-4844c49"
https://www.wix.com/code/reference/wix-users.User.html#id
Also note:
We need to choose a field to add to the URL that uniquely identifies each member. Here we use each member's automatically generated IDs to uniquely identify them.
With yourself wanting to use the users username instead, then you are open to having duplicate usernames as anybody can have the same username.
Then you will have to add more code to your page, also adding a data file to your backend as well, to make sure that when somebody creates a username, that the username is not already in use and is not duplicated again.
See here:
https://www.wix.com/code/home/forum/wix-tips-and-updates/example-no-database-duplicates
or
https://codequeen.wixsite.com/membership-dashboard
https://www.youtube.com/watch?v=yLCOqsVHhD0 (video for above tutorial)
This is the full code from the tutorial that you have used, simply add your own dataset names and once you have also setup your dynamic pages it should take the logged in user to their own profile page where they can update their own profile by using the update dynamic page as shown in the tutorial.
import wixUsers from 'wix-users'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; $w.onReady( () => { if(wixUsers.currentUser.loggedIn) { $w("#loginButton").label = "Logout"; $w("#profileButton").show(); } else { $w("#loginButton").label = "Login"; $w("#profileButton").hide(); } } ); export function loginButton_click(event) { //Make sure that onclick event is on in properties // user is logged in if(wixUsers.currentUser.loggedIn) { // log the user out wixUsers.logout() .then( () => { // update buttons accordingly $w("#loginButton").label = "Login"; $w("#profileButton").hide(); } ); } // user is logged out else { let userId; let userEmail; // prompt the user to log in wixUsers.promptLogin( {"mode": "login"} ) .then( (user) => { userId = user.id; return user.getEmail(); } ) .then( (email) => { // check if there is an item for the user in the collection userEmail = email; return wixData.query("DatabaseName") // Your own dataset name here .eq("_id", userId) .find(); } ) .then( (results) => { // if an item for the user is not found if (results.items.length === 0) { // create an item const toInsert = { "_id": userId, "email": userEmail }; // add the item to the collection wixData.insert("DatabaseName", toInsert) // Your own dataset name here .catch( (err) => { console.log(err); } ); } // update buttons accordingly $w("#loginButton").label = "Logout"; $w("#profileButton").show(); } ) .catch( (err) => { console.log(err); } ); } } export function profileButton_click(event) { //Make sure that onclick event is on in properties wixLocation.to(`/DatabaseName/${wixUsers.currentUser.id}`); // Your own dataset name here }
If you want to change the id to another field in your dataset then when you set your dynamic page url, simply delete the existing field they have used in the url and click on add field to use your own field choice instead from your dataset.
Then basically the url will be changed instead to username or name or whatever field you have chosen and you can adjust it as so in the page url for wix location.
Still stuck, then watch this walk through video - https://www.youtube.com/watch?v=myKhmcNR0-s&t=2s