Checking if Boolean in collection is true then showing an element

Hello,

This code is checking if the Boolean in collection is true or not based on the user.

That code works but I just need a little help to make this code shorter. At the moment it’s doing new query every time its checking the collection field. I think there should be a better way to do only one query and check different fields in collection.

Does anybody have an idea?

//Check Applicant Details Step
  $w("#dynamicDataset").onReady( () => {
 const userId = $w("#dynamicDataset").getCurrentItem()._id; 
    wixData.query("Collection")
      .eq("_id", userId)
      .eq("applicantDetails", true)
      .count()
      .then ((results1) => {
 if (results1===1) {
              $w('#applicantDetailsCompleted').show();
          }
 else {
            $w('#applicantDetailsCompleted').hide();
          }
    })

 //Check Pre-Survey Step
     wixData.query("Collection")
      .eq("_id", userId)
      .eq("preSurvey", true)
      .count()
      .then ((results1) => {
 if (results1===1) {
              $w('#preSurveyCompleted').show();
          }
 else {
            $w('#preSurveyCompleted').hide();
          }
    })

 // Check Faucets Step
    wixData.query("Collection")
      .eq("_id", userId)
      .eq("faucets", true)
      .count()
      .then ((results1) => {
 if (results1===1) {
              $w('#faucetsCompleted').show();
          }
 else {
            $w('#faucetsCompleted').hide();
          }
    })
})

You will need something like this one… (not tested)

console.log ( xxx("Collection", "applicantDetails", "applicantDetailsCompleted", "applicantDetailsCompleted" ) )
console.log ( xxx("Collection", "preSurvey", "preSurveyCompleted", "preSurveyCompleted" ) )
console.log ( xxx("Collection", "faucets", "faucetsCompleted", "faucetsCompleted" ) )


function xxx (COLLECTION, REFERENCEFIELD, VALUE1, VALUE2) {
 // Check Faucets Step
    wixData.query(COLLECTION)          //<---------------------------COLLECTION
    .eq("_id", userId)
    .eq(REFERENCEFIELD, true)          //<---------------------------REFERENCEFIELD
    .count()
    .then ((results1) => {
 if (results1===1) {$w('#'+VALUE1).show();}      //<---------------VALUE1
 else {$w('#'+VALUE2).hide();}                   //<---------------VALUE2
 return
    })
}

This will use always the same QUERY-STRUCTURE, but always you can change your values and push them to the function “xxx”.

Thanks!

I came up something like this and it worked.

 $w.onReady(function(){ 
   $w("#dynamicDataset").onReady( () => {
 const userId = $w("#dynamicDataset").getCurrentItem()._id; 
   wixData.query("Collection")
   .eq("_id", userId)
   .find()
   .then( (results) => {
 let applicantDetails = results.items[0].applicantDetails;
 let preSurvey = results.items[0].preSurvey;
 let faucets = results.items[0].faucets;

 if(applicantDetails === true){
      $w('#applicantDetailsCompleted').show();
      } 
 else{ 
      $w('#applicantDetailsCompleted').hide();
      }
 if(preSurvey === true){
      $w('#preSurveyCompleted').show();
      } 
 else{ 
      $w('#preSurveyCompleted').hide();
      }
 if(faucets === true){
      $w('#faucetsCompleted').show();
      } 
 else{ 
      $w('#faucetsCompleted').hide();
      }
    })
    .catch( (err) => {
 let errorMsg = err;
    });
  })
})

Ok, also a solution. The mainthing is, that it works for you.
There are always several ways of solution, which you can take.
Well done!

Good luck and happy coding.:wink: