How to hide/show a strip based on database entries?

I am almost certain my code was correct and working previously, but I haven’t tended to this site in a while, and now it appears to not be working.

I am trying to hide Strip1 and show Strip2 if a user has already completed the form.

I have tried several code variations to achieve the same outcome, but none seem to be working. Please can someone tell me where the problem lies in my code?

I have reached out on for help from a Wix Professional, but it’s been 5 days and I haven’t had a response.

Any help would be greatly appreciated :heart:

Thank you

P.s. Apparently I can’t post “links” on this forum because my account is new… I’ve been a Wix customer for over 8 years… how can my account be new? Anyway, I’m not allowed to paste the code in, so please forgive the screenshot and cramped code (I had to make it fit))

I see some things that need to improve in your code, can you send it to my email bwprado@gmail.com?

Thanks

Lifesaver! Thank you for responding - I’ve sent you an email :ok_hand:

Hello @saganwork , I took a look at your code and the only really wrong thing missing is the parentheses in you function call. But your code is in desperate need of a refactoring (rewriting some parts of it to make it clearer).

$w.onReady(function () {
    toggleEntry() //This is wrong in your code
})

I would do something more like this:

$w.onReady(() => {
    $w("#dbComp").onReady(() => { //You call the dataset onReady after the page is ready
        toggleEntry() //Then you call your function
 })
})

function toggleEntry() {
 let currentUser = $w("#dbComp").getCurrentItem().entered
 if (currentUser === "Yes") {
        $w("#columnStrip1").hide()
        $w("#strip2").show()
 } else {
        $w("#columnStrip1").show()
        $w("#strip2").hide()
 }
}

The last function attached to your submit button is something that I could not understand (functionally) properly. I got what it is doing, but not why.

Hi Bruno,

Thanks for your answer, but it doesn’t work. Both strips are always showing. I also tried to enable/disable a button instead, but it also doesn’t work - the button is always enabled .

import wixUsers from “wix-users” ;
import wixData from “wix-data” ;
import wixLocation from “wix-location” ;

$w . onReady (() => {
$w ( “#dbComp” ). onReady (() => {
toggleButton ()

})
})

function toggleButton () {
let currentUser = $w ( “#dbComp” ). getCurrentItem (). entered
if ( currentUser === “Yes” ) {
$w ( “#btnSubmit” ). disable ()
} else {
$w ( “#btnSubmit” ). enable ()
console . log ( currentUser )
}

}

I also tried this way - but it also doesn’t work;

function checkEntries(){

var user = wixUsers.currentUser;

wixData.query(“CompetitionEntrants”)
.eq(“_id”, user.id)
.find()
.then((result) => {

      if(result.items.length > 0){ 

          var item = result.items[0]; 

          if(item.entered === "Yes"){ 
              $w("#columnStrip1").hide(); 
              $w("#strip2").show(); 
          } else { 
              $w("#columnStrip1").show(); 
              $w("#strip2").hide(); 
          } 
      } 
  }); 

}

When I perform a console.log(currentUser), the developer panel shows “undefined” whether there is an entry in the database or not.

There seems to be an issue with Wix reading the database.

I have double and triple-checked that my field keys are correct, the names of all the elements and databases are correct, and still this is not working.

As I said, I am willing to pay for someone to help me with this as I am desperately running out of time. I have applied on the Wix Partner page, but have not had an answer in over a three weeks.

Hi Bruno,

Performing a console.log on “currentUser” returns the following message:
TypeError: Cannot read property ‘entered’ of undefined

Does this help clarify where the problem lies?

Thank you

Hi there,

There are a lot of posts/threads about this topic, please observe the forum for similar questions before posting new ones.

First of all, you need to wait until you get the item you want to evaluate from the database or the dataset, this might sound obvious, but I’m sure a lot of people don’t see it this way.

Database Scenario

import wixData from 'wix-data';

$w.onReady(() => {
    return wixData.query('collection')eq('field_name', value).find().then(x => {
        const item = x.items[0];
        
        // Run the evaluation here
    })
})

Dataset Scenario

$w.onReady(() => {
    $w('#dataset').onReady(() => {
        const item = $w('#dataset').getCurrentItem();
        
        // Run the evaluation here
    })
})

Now that we have the item that we need to evaluate, we can use a simple if statement to check any field

// Example 1: Check a boolean value
if (item.premium) {
    // Show premium content
    $w('#premiumStrip').show();
} else {
    // Show regular content
    $w('#regularContent').show();
}


// Examlpe 2: Show exclusive content for those with 10+ score
if (item.score >= 10) {
    // Show exclusive content    
} else {
    // Show a message
    $w('#message').text = 'You need higher score to get access...';
}

Hope this helps~!
Ahmad