Using Include() function with multi-reference fields

Hi, can anyone tell me if it’s possible to use the include() function to query multiple reference fields ? And if so, can you please tell me what I’m doing wrong?

I have two tables with a many-to-many relationship and I really need to be able to actually query Table A by references to Table B.

Example : Table A contains event photos, Table B contains categories. I have a query that runs when users enter search terms. If a user enters a category (ex. weddings), any event photos with the term “weddings” in it’s category reference field would be returned.

I’ve searched all over and I can’t find an answer as to whether or not it’s actually possible to use the include() function in this way, but it seems to be my best bet. From what I can tell, you should be able to use it if a collection has only one multiple reference field , but I can’t get it to work.

I’d really appreciated some help with this - this is the one thing that’s completely holding me up with using data collections in my site.

Here is what I have tried so far to accomplish a query on a multiple reference field:

  • Using the Include() function in my query to include the reference field

  • Using query referenced to try and query the referenced items

  • Concatenating my results, which I don’t think is really what I’m going for

  • Using hooks to combine fields and create one long string of searchable terms, including those found in the reference field - however, reference fields only appear as “Object” when they are copied to a text field.
    Here’s the relevant code, with my current efforts to use Include():

export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”) <— Table A (EventPhotos)
.include(“categories”) <— reference field to Table B (Categories)
.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(); 
        console.log(results) 
    }); 

I’m feeling really discouraged - I’ve been working on this for a while now with no success, I would LOVE some help with this.

#includefunction #query #multireference #multireferencefield #multiplereference #multiple #reference #queryreferenced #concatenate

@yoav-wix Can you help? I saw your post about using the Include() function, do you have any ideas about this? Thank you in advance for your help!

Hay @elena,

Multiple references are a bit tricky in terms of usage. What you should be doing is using queryReferenced, in the following way -

for a single category, you should do

res = await wixData.queryReferenced("categories", $w("#searchbox").value, "EventPhotos");
// res includes the referenced photos 

where “EventPhotos” is the field name in “categories” for the multi-reference to the “EventPhotos” collection

Hey @yoav-wix , thank you so much for responding!

So, where exactly in my code would I use that? Would I still need to have a separate query for the event photos themselves? Or would I replace my current query with the query referenced function?

@elena the function returns the list of referenced photos. You just set those items to the table.data property

@yoav-wix Okay, meaning that I would populate my results repeater with each event photos’ referenced categories? But then will that allow users to search both by category AND by other fields in the event photos database? (Such as description or name).

@yoav-wix Hi, I’m still struggling with this… I don’t know much about java at all, obviously. Can you please just show me an example of how I should utilize the queryreferenced function within my code? I would REALLY appreciate it.

Thanks so much!!

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")
        .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"))
        });

Hi @yoav-wix I read the Wix API and now your advice, but it doesn’t work for me:

let res = await wixData.queryReferenced("categorie", "cuoco, "categs");
console.log(res.items)

result:
Error: Item [cuoco] does not exist in collection [categorie].

OK I found the answer!

I was also looking the Yisrael post:
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-wix-data-multiple-references
and I discovered, in despite of what API suggest:

queryReferenced( )
function queryReferenced(collectionName: string, item: Object | string, propertyName: string, options: WixDataQueryReferencedOptions): Promise<WixDataQueryReferencedResult>
...
item Object | string
The referring item or referring item's ID.

that you can use only the referring item’s ID, you can’t use the item text.
At least I so understood, but it’s not so clera the differences with include.
Anyway