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…
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.
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.
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.