Limit search on collection data to only show exact match, not partial matches

Evening all.

I followed the guide below on adding collection data search functionality and everything works.

https://support.wix.com/en/article/corvid-tutorial-adding-collection-data-search-functionality

However, partial search terms are showing more data than they should.

As an example, I am searching a collection of customer contract pricing. Different customers have different contract pricing for the same product.

Imagine I had 2 rows in this collection with the column headers of:

Article code
Description
Code
Price

The article code and description in each of the two entries are the same with only the code and price changing.

Row 1 has code = “Code1” with a price of $2.00.
Row 2 has code = “Code2” with a price of $3.25.

Presently, the user can just enter “Cod” into the search box and this returns ALL results.

How do I stop the results from showing all entries in my collection when a partial code is entered and instead show a message on the screen such as “Code not recognised”?

import wixData from "wix-data";
//https://support.wix.com/en/article/corvid-tutorial-adding-collection-data-search-functionality

export function button1_click(event) {
// Runs a query on the "ProcureASSISTv1" collection
wixData.query("ProcureASSISTv1") 
 // Query the collection for any items whose "code" field contains  
 // the value the user entered in the input element
  .contains("code", $w("#CodeInput").value)
  .find()  // Run the query
  .then(res => {   
 // Set the table data to be the results of the query     
    $w("#table1").rows = res.items; 
   });
}

$w.onReady(function () {
 $w("#table1").columns = [{
 "id": "image",
 "dataPath": "image",
 "label": "Image",
 "type": "image",
 }, {
 "id": "description",
 "dataPath": "description",
 "label": "Description",
 "type": "string",
 }, {
 "id": "colour",
 "dataPath": "colour",
 "label": "Colour",
 "type": "string",
   }, {
 "id": "articleCode",
 "dataPath": "articleCode",
 "label": "Article Code",
 "type": "string",
     }, {
 "id": "price",
 "dataPath": "price",
 "label": "Price",
 "type": "string",
 }];
});

Hello alanboyle80

change this …

.contains("code", $w("#CodeInput").value)

…to this…

.eq("code", $w("#CodeInput").value)

I think this should solve the issue.

Brilliant russian-dima, thanks. That works.

Can you assist with the second part of my query?

If a partial code is submitted and not found how can I have a message automatically appear somewhere on screen and say “Incorrect customer code”?

If you take a look, here at this link…

https://www.wix.com/corvid/reference/wix-data.html#query

…you will find how to make a data-query the right way.

You have forget something!

wixData.query("myCollection")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let firstItem = results.items[0]; //see item below
    } else {
      // handle case where no matching items found
    }
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

What do this part?

if(results.items.length > 0) {
      let firstItem = results.items[0]; //see item below
    } else {
      // handle case where no matching items found
    }

Especialy this part…

if(results.items.length > 0) {...............

If items were found, then the lenght of the result has to be grather then 0, right?
And now think about in :wink: