How do I convert a reference field into a searchable string in a query?

I would like my search functionality to search through 4 different fields. It’s working perfectly for 2 fields, but not the referenced field. I tried .toString() but it does not seem to work.

Here is the code:


export function searchButton_click(event) {
    
    wixData.query("Vendors")
    .contains("name".toLowerCase(), $w("#searchInput").value.toLowerCase())
    .or(
        wixData.query("Vendors")
        .contains("skill1".toString().toLowerCase(), $w("#searchInput").value.toLowerCase())    //NOT SEARCHING SKILL1
    )
    .or(
        wixData.query("Vendors")
        .contains("skill2".toString().toLowerCase(),     $w("#searchInput").value.toLowerCase())     //NOT SEARCHING SKILL2
    )
    .or(
        wixData.query("Vendors")
        .contains("city".toLowerCase(), $w("#searchInput").value.toLowerCase())
    )
    .find()
    .then(res => {
        $w("#repeaterVendors").data = res.items;
    }); 

}

The search is working for “name” and for “city” but not for “skill1” and “skill2”, which are both referenced fields. (The foreign referenced fields are Strings. But I am converting with toString() and still not working.
Any advice?

Hi there :wave:t2: To query referenced fields try using either the include() function , or the queryReferenced() function .

You can check out this article to see which might fit your uses better.

Good luck!

Hi there,

You cannot query nested data into a reference field with a query alone, when you include your reference field in a query, it’s treated as a " TEXT " field, meaning that you need to provide the item ID ( _id ) in order for the query to get these items.

Assuming we have a database with the following fields:

  • Title ( TEXT ).
  • Member ( Reference Field ).
  • Data ( Object ).
// Data object schema
/*
{
    name: 'Ahmad',
    website: 'nasriya.net',
    company: 'Nasriya Software'
}

You can query nested data inside the “Data” field this way:

query('col').eq('data.company', 'Nasriya Software').find();

// This will return 1 item in the result.

But you cannot query nested data inside a reference field - even if you included it in the query - even if it’s an object.

query('col').include('member').eq('member.name', 'Ahmad').find();

// This will return 0 item in the result.

Hope this helps~!
Ahmad