Repeater showing all records when dropdown value is undefined

Hello,
I am filtering a dataset (“Cities”) based on the value entered in the dropdown box (#citypick1). The outcome should be shown in the repeater (#citystrip).

The code works fine when a value is selected. However, if no value is selected in the dropdown box, I would like the repeater to show one specific record of the dataset. I am trying to amend the below code to make this happen as currently the outcome is that it shows all the records included in the dataset when no value is selected, instead of a specific record.

wixData . query ( “Cities” )
. contains ( “title” , String ( $w ( ‘#citypick1’ ). value )) // filters field “title” in the dataset
. find ()
. then ( results => {
if ( results . items . length > 0 ) { // if there are results matching the query…
$w ( ‘#citystrip’ ). data = results . items ; //…show the filtered result
//this
} else { // if there are no results matching the query…
$w ( ‘#citystrip’ ). data = results . items [ 1 ]; // show record 2 of the dataset
}
});

Any help would be greatly appreciated.
Alessandro

Hi! Try out:

wixData.query("Cities")
.contains("title", String($w('#citypick1').value)) // filters field "title" in the dataset
.find()
.then(results => {
  if ($w('#citypick1').value === '') { // if no value is selected in the dropdown box
    wixData.get("Cities", "6a8a6759-5e5d-4f1e-8c7a-9b6a44c4f2d4") // get a specific record by its ID
      .then(record => {
        $w('#citystrip').data = [record]; // show the specific record in the repeater
      });
  } else if (results.items.length > 0) { // if there are results matching the query...
    $w('#citystrip').data = results.items; //...show the filtered result
  } else { // if there are no results matching the query...
    $w('#citystrip').data = results.items[1]; // show record 2 of the dataset
  }
});

Many thanks bamuu. Quick one, how do I get the specific record ID from the dataset ? (equivalent of 6a8a6759-5e5d-4f1e-8c7a-9b6a44c4f2d4)

One way to get an ID for a particular item in your collection is to look in the collection table. Make sure on “Manage Fields” you have ID toggled to show