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
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
@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!