Hi, without going into detail about how backwards I’ve gone about this, could someone help me link a button to a database url through example code?
Essentially, my Wix Members’ lastName field equals a unique external website, which I’m trying to somehow link in the below style code when a button is pressed to the logged in user:
export function iconButton1_click(event) {
wixLocation.to("lastName");
}
But I don’t know how to code this in - should it be wixLocation.to? is the lastName referecning correct? Does the fact that the database field lastName is a text/string make this impossible, or how can I convert it during this process?
Hoping a code workaround is feasible, because I’ve limited time to revamp the whole project from scratch when it wasn’t working before following the plethora of wix tutorials, leaving this to be my workaround.
That’s the full code… This is my first / only attempt at retrieving it.
Edit:
Wait, I do have the other essentials which I used for the login section to. Of course the import/$w.onReady aspects are required. Straight from a 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) {
// 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("Profile")
.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("Profile", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#profileButton").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function iconButton1_click(event) {
wixLocation.to("_lastName");
}
@eduardosantos_92 I don’t understand this line: wixLocation.to(“_lastName”);
Do you have a page path “_lastName”? (if you wanted to redirect to specific last name, you had to get it first)
If you do have a “_lastNa,e path, then just add a slash before:
wixLocation.to(”/_lastName");