I have a page in the members area which takes the logged in user’s email, uses it to look up a link in the CMS. This link I then want to open in a new tab. Here is my code (I also assign a couple of text fields with the CMS data just to prove connection works). The issue lies in $w(‘#button3’).link = entry.link - I was hoping this would open the link:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady(function () {
async function button3_click(event) {
let user = wixUsers.currentUser;
let userEmail;
if (user.loggedIn) {
try {
// Get the user's email using async/await
userEmail = await user.getEmail();
console.log('user email', userEmail);
$w('#text51').text = userEmail;
// Query your CMS collection based on the user's email
wixData.query('ExceldocumentLinks')
.eq('email', userEmail)
.find()
.then(results => {
// Check if any entries were found
if (results.items.length > 0) {
// Access the first entry
const entry = results.items[0];
// Now you can use the entry data as needed
console.log('CMS Entry:', entry);
$w('#text52').text = entry.link;
$w('#button3').link = entry.link;
} else {
// Handle the case where no entry was found for the constant email value
console.error('No CMS entry found for the constant email value');
}
})
.catch(error => {
// Handle errors during the query
console.error('Error querying CMS:', error);
});
} catch (error) {
console.error('Error getting user email:', error);
}
} else {
// Handle the case where the user is not logged in
console.error('User not logged in');
}
}
// Assign the click event handler to the button
$w('#button3').onClick(button3_click);
});
I tried wixlocation but I believe this isn’t approprate in this case. I also tried custom element but couldn’t find any specific guidance or resources to this type of request (I am relativey new to Velo so learning as I go).
Any help or links would be much appreciated.