Help finishing off code to check a database...

I need help finishing off this code please. I want to check a database (linked to some dynamic pages) called prospects under the field called showMenu which will have a boolean entry - true or false.

If it’s true I want a strip called menuStrip to show and if it’s false for it not to. The error message is saying that ‘item’ is not defined.

Thanks for your help!!

Hi Clayton,

You will need to add some criteria in your query to specify exactly what record in prospects that you want to obtain data from. I’m using a facetious condition asking that the fullName field = “John Doe”.

$w.onReady(function() {
  wixData.query("prospects")
  .eq("fullName", "John Doe")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let item = results.items[0];
      if (item.showMenu === true){
        $w("#menuStrip").show();
      }
    } else {
      // nothing found
    }
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  });
});

Thanks Anthonyb for your reply. I really appreciate it. Still not right unfortunately. The page (which is dynamic) isn’t acting as hoped, with the “menuStrip” still hidden even if the ‘showMenu’ item is true.

I’ve double checked the properties panel to see that everything is named correctly and have sync’d the to the live database.

I have a field called businessName and there’s a business listed in that field called Shine. Not sure if that’s how you intended that bit of code to be used… ideally when you go to each prospects dynamic page it’ll show a certain strip only if it’s marked true in the prospects database.

Cheers Clayton

@claytonrmorgan The first thing to do here is to determine if the query is returning any results.
To facilitate that, could you put a console.log(results) right after the .then line? If you don’t see a three-dot button to expand in the console, the query is not producing any results. If that’s the case, it could be one of several reasons.

  1. The collection name is in the wrong case.

  2. The field name is in the wrong case (probably not because you have a mixed case “businessName”)

  3. Somehow the data was entered with trailing spaces. In other words, that would mean businessName value of "Shine " was not exactly equal to “Shine”. Using .contains instead of eq. would solve that.
    If the query is returning results, verify that the “collapsed on load” is not checked for the menuStrip.

@anthonyb thanks for this. The query is returning results. The menuStrip was ticked “collapsed on load” and I’ve unclicked that now, and have published but it’s still not working right. I can check or uncheck the showMenu boolean in the database and it makes no difference to the menuStrip showing or not showing - it’s always showing for every entry including “Shine”.

I was thinking that you had the “hidden on load” ticked if you were showing the strip only in cases where showMenu is true, but maybe you don’t? With your approach here, it should be ticked. However, maybe what you really want to use is expand and collapse rather than show and hide. Are there elements below that you want to “slide up” if the menuStrip is not showing? If so, you would want to have only “collapsed on load” ticked, and when showMenu is true, then the line would be:

$w("#menuStrip").expand();

@tony-brunsman thanks man, that’s what I was thinking to, but still not working right…

Because dynamic pages are going to source this, will I have to code in and find each “businesName” listed in the database before it can then do the true or false part?

Hey @claytonrmorgan
Can you paste the final code here and

I have update the code with console log
Let us know if there is a “item”
and if the “Menu strip showing…”
logged

$w.onReady(function() {
  wixData.query("prospects")
  .eq("fullName", "John Doe")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let item = results.items[0];
      console.log(item);
      if (item.showMenu){
      console.log("Menu strip showing...")
        $w("#menuStrip").show();
      }
    } else {
      // nothing found
    }
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
    console.log("Error while query : " , error)
  });
});