Help Problem: .contains needs if statement to check number of characters value.length

I’m literally one line, maybe two away from solving this conundrum.

abb is 3-5 characters long, yes I could force them to be all three letters and eliminate the problem, but for dumb reasons, I’m not going to.

Basically I have a table. (its a serial number
[title] [abb]
MSG1000 MSG
MSG1002 MSG
MSG1003 MSG
MS1000 MS
MS1001 MS
MS1002 MS

I need to search abb within title. Which is great, except if I search for MS, I will pick up all the MSG values using .contains .
I need to add a let statement, but i’m not sure how to write it
let titlelength = “title”.value.length;

I know I need an if statement in here. Because I need check to see if titlelength = abblength, and if it does get a total count.

import wixData from 'wix-data';
export function dropdown1_change(event) {
    let searchabb = $w('#dropdown1').value;
    let abblength = $w('#dropdown1').value.length + 4; 
    const filterValue = searchabb;
    const byTitle = wixData.filter().contains("title", filterValue);
    $w("#dataset2").setFilter(byTitle)
        .then(() => {
        let totalCount = $w("#dataset2").getTotalCount() +999;
        $w('#input1').value = searchabb + totalCount;
    });
}

Hi,
I see that you are using dataset filter.

I would recommend querying the database itself and apply filters to it. You can see the example code below:

import wixData from 'wix-data';

// ...
if ($w('#dropdown1').value === "MSG") {
wixData.query("myCollection")   
.contains("title", filterValue)
.find()   
.then( (results) => {     
if(results.items.length > 0) {       
let items = results.items;     
} else {       
// handle case where no matching items found     
}   } )   
.catch( (error) => {     
let errorMsg = error.message;     
let code = error.code;   } );
}
else {
wixData.query("myCollection")
  .contains("title", filterValue)
  .not(
    wixData.query("myCollection")
      .contains("title", "MSG")
  )
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let items = results.items;
    } else {
      // handle case where no matching items found
    }
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );
  }

Please note that the code above is an example and you need to adapt it to your database collection name and get the desired values from the results.

Hey, thanks for this. I actually ended up working my way around this in a very different way.

Basically I had to create a new column in my table. To designate the owner “MSG”. Then i had the database count the number of “MSG” within the database this forced it to count only exact and MS wouldn’t be counted, and then add 100. then i had it concatenate MS + (count + 100). if anyone needs me to get the code, I can certainly