Redirect to other URL if boolean value in collection = false (RESOLVED)

Hello community,

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.

#text86’ never changes, regardless of scenario.

Any suggestions please?

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?

Hi Yisrael, any update please?

You have two page onReady() functions which is invalid and results in unpredictable behavior.

Furthermore, you need to have a Dataset.onReady() function within the page’s onReady() function as the Dataset might not yet be ready when the page is. See the forum post Datasets have an onReady function for more information.

Much appreciated. I shall try those amendments.

Hello Yisrael,
Your suggestions pointed me in the right direction and I found a solution. It now works perfectly. Here is the code:

$w.onReady( () => {
$w(“#dataset1”).onReady( () => {
//console.log(“The dataset is ready”);
if (wixUsers.currentUser.loggedIn) {
setButtons( true );
let url = wixLocation.url;
if (url === “https://www.essence-rooms.com/services”) {
let tandcs = $w(‘#dataset1’).getCurrentItem().tandcs;
if (tandcs === true ) {
//console.log(“T&C Are Signed”);
}
else {
//console.log(“T&C Are NOT Signed”);
wixLocation.to(/Users/${wixUsers.currentUser.id});
}
}
}
else {
setButtons( false );
}
})
})

Huge thank you again!!! Oberdan

@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!