Convert a reference field to text?

Does anyone know if it’s possible to convert a reference field to a text string? All I’m trying to do is to get my query to be able to find search terms in a particular database’s reference fields. For example, I have an “event photos” collection and a search box set up to show my query results in a repeater. Works great, except I also need for it to be able to turn up results based on text in a reference field. I have another collection called “Categories,” and I just want users to be able to type in a search term, and if an Event Photo has a related “category” that matches that search term, it’ll come up in the results.

I’m at kind of a loss as to why this should be such a complex task! Any ideas? I can’t imagine I’m in a unique situation here…

Thanks in advance for any help!!!

Oh, and this is the code so far. The bolded words “categories” is the field key of the reference field. I’ve tried using an include function, and I’ve played around with combining multiple collection queries, but I don’t really want to display multiple collections, I just want to display the one collection, but be able to search/filter it by its reference fields.

import wixData from ‘wix-data’;

$w.onReady(function () {
$w(“#searchedrep”).onItemReady(($item, itemData, index) => {
$item(“event”).text = itemData.relatedjob;
$item(“#description”).html = itemData.jobdescription.substring(0, 225) + ‘…’;
$item(“#thumbnail”).src = itemData.thumbnail;
$item(“#thumbnail”).link = itemData[‘link-EventPhotos-relatedjob’];
$w(“#thumbnail”).clickAction = “link”;
})
});

export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”)
.contains(“relatedjob”, $w(“#searchbox”).value)
.or(wixData.query(“EventPhotos”)
.contains(“jobdescription”, $w(“#searchbox”).value))
.or(wixData.query(“EventPhotos”)
.contains(" categories “, $w(”#searchbox").value))

    .find() 
    .then((results) => { 
        $w("#searchedrep").data = results.items; 
        $w("#searchedrep").expand(); 
    }); 

}

Hi,
Check out this post about the include() addition to query, using the include, the returned items will have the data of the referenced items.

Good luck :slight_smile:
Or

Hi, do you know if there’s a way to get the include function to work with a multiple-reference field? I haven’t found a way yet…

Hi, just checking in again about the include() function, I can’t get it to work with a multiple-reference field… I appreciate your help!

Hi,
You can use include() to get multiple reference fields by adding the name of the referenced collections:

import wixData from 'wix-data';

// ...

wixData.query("books")
  .include("author", "publisher")
  .find()
  .then( (results) => {
    if(results.items.length > 0) {
      let books = results.items;
      let firstBook = items[0];
      let firstAuthor = firstBook.author;
      let firstAuthorBio = firstAuthor.bio;
      let firstPublisher = firstBook.publisher.name;
    } else {
      // handle case where no matching items found
    }
  } )
  .catch( (error) => {
    let errorMsg = error.message;
    let code = error.code;
  } );

In this example there is a books collection which has two reference fields that reference an authors collection and a publishers collection.
The query includes the author and publisher properties so that each returned book will also include the full items of its referenced author and publisher.
Check out the API here.

Good luck :slight_smile:
Or

1 Like

Hi Or, I’ve tried doing this without any luck… Can you tell me what I’m doing wrong? All I really want is for the query to work by searching terms found in the referenced field. I don’t particularly care about it returning results from the referenced field, if that makes sense. For example, if a user searches “corporate” (a term that would be found in the referenced “categories” field), for the query to return all results that include the word “corporate” in its referenced categories field.

Here’s my code so far.

export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”)
.include(“categories”)
.contains(“relatedjob”, $w(“#searchbox”).value)
.or(wixData.query(“EventPhotos”)
.contains(“jobdescription”, $w(“#searchbox”).value))
.or(wixData.query(“EventPhotos”)
.contains(“categories”, $w(“#searchbox”).value))
.or(wixData.query(“EventPhotos”)
.contains(“tags”, $w(“#searchbox”).value))

    .find() 
    .then((results) => { 
        $w("#searchedrep").data = results.items; 
        $w("#searchedrep").expand(); 
        console.log(results) 
        console.log(Object.values("categories")) 
    }); 

}