I am looking to redirect logged in users who have yet to complete their members profile when they visit the bookings page (https://www.essence-rooms.com/services).
Here is the code:
$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
let url = wixLocation.url;
console.log(url);
if (url === “https://www.essence-rooms.com/services”) {
let tandcs = $w(“#dataset1”).getCurrentItem().tandcs;
console.log(tandcs);
if (tandcs === true ) {
$w(‘#text86’).text = “True”;
} else {
$w(‘#text86’).text = “False”;
wixLocation.to(/Users/${wixUsers.currentUser.id});
}
}
}
})
Once logged in and clicking on https://www.essence-rooms.com/services console.log(tandcs) shows this message “TypeError: null is not an object (evaluating '$w(”#dataset1").getCurrentItem().tandcs’)" and the page does not redirect.
However, if I then refresh the page (F5) console.log(tandcs) shows ‘undefined’ and the page is correctly redirected.
It looks like you have currently “commented out” this code. In any case, a couple things to keep in mind:
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.
You should use the onReady() function to make sure that the dataset is ready when the page has loaded.
Thanks Yisrael. I did comment the code out as it is a live site and I was worried I did something to severely impact its functionality.
The results I listed above were all within the live, published site. I removed the “commenting out” and made the code active, in case you wish to check it again.
I wrote the code within the onReady function, but I am assuming you mean something different?
@oberdan thank you for posting your code. This is the exact feature that I’m also needing to have. Would you mind posting screenshots of your code as I find it difficult to follow when typed. Thank you!