I am trying to program a button on a dynamic page to link to an external page based on field values of the current user.
Specifically, each user has a database field called “myurl” in a database called “Members”. This field is populated with an external web address (e.g. www.myexternalwebsite.com)
I have included my code below. The button is not functioning, so clearly I am doing something wrong.
Thanks for your help!
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady(() => {
let myurl = wixUsers.currentUser.myurl;
if (myurl)
$w(‘#button1’).link =
‘https://’ + myurl;
})
Hi,
The property myurl is not a property of currentUser . You will need to get the record from your Members collection. Get the userID and you can then use it like this:
let userId = wixUsers.currentUser.id;
wixData.get("Members", userId)
.then( (results) => {
let item = results; //see item below
$w('#button1').link = results.myurl;
} )
.catch( (err) => {
let errorMsg = err;
} );
I hope this helps,
Yisrael
That makes sense, thanks Yisrael. I copied in your code as you directed. So now my code reads:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady(() => {
let userId = wixUsers.currentUser.id;
wixData.get(“Members”, userId)
.then( (results) => {
let item = results; //see item below
$w(‘#button34’).link = results.myurl;
} )
. catch ( (err) => {
let errorMsg = err;
} )
});
However, the button is still not functioning for some reason. Are you able to look at my code at the dynamic page: https://dkbaxter.wixsite.com/website-3/welcome/davidbaxter ?
Many thanks!
I looked in the Members collection and the myurl field is empty for all entries in the collection. The .get() appears to work, but there is no myurl to use for the button link.