Anyone? A good walk thru from beginning of setting up the multiple item reference fields to actually using such as searches of those fields, displaying items from the multiple item field in a repeator, etc. Limitations of use and actually conceptual goal of their use?
@jeff-haskins just curious if you ever got any more information about this, I’m currently struggling with trying to get my database query to include a reference field (or two) in its search terms.
@jeff-haskins Just circling back again.
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
Or
Hi Or, I’ve actually tried this but with no luck… Can you tell me what I’m doing wrong? Do I need to set it up to run two queries?
export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”)
.include(“categories”) <------- referenced field
.contains(“relatedjob”, $w(“#searchbox”).value)
.or(wixData.query(“EventPhotos”)
.contains(“jobdescription”, $w(“#searchbox”).value))
.or(wixData.query(“EventPhotos”)
.contains(“categories”, $w(“#searchbox”).value)) <------- me attempting to get it to query the referenced field
.find()
.then((results) => {
$w("#searchedrep").data = results.items;
$w("#searchedrep").expand();
console.log(results)
console.log(Object.values("categories"))
});
}