Query using a Multi-reference Field

I’ve been coding applications for many years in a form of Basic, however I am new to both Wix coding and Java Script.
I have two collections with a many-to-many relationship. A. is a Category collection (business sectors), B. is a Business collection (individual companies).
Collection A. has a multi-reference field called Business which includes all the Businesses associated with a given Category.
I wish to display all the businesses for a selected Category using the queryReferenced() statement:
let names = await wixData.queryReferenced(“Category”, $w(“#Category”).value, “Business”);
“Category” = Collection Name
#Category” =Selected Category
“Business” =Multi-reference field name
names =list of business names in the Business multi-reference field
Are the above parameters correct?
I am using a repeater field to display the businesses.
Can anyone show me what the code looks like to insert the ‘names’ variable into the page to display the businesses using the repeater field.

Thanks in advance.

Gerry

Hi,

You can use a simple query function and pass ID of the categories you want, without having to use queryReferenced. Have you tested it yet?

It’s something like this:

import wixData from 'wix-data';
wixData.query("business")
   .eq('categoryReferenceField', 'categoryId')
   .find()
   .then((results) => {
       console.log(results.items)
   })
   .catch((err) => {
let errorMsg = err;
   });

So, you can learn more about Query functions here: wix-data - Velo API Reference - Wix.com

Hope it helps :wink:

Here’s an example that will certainly help:

Multi-Reference Query

Query multiple-item reference fields and display the results in a table.

Many thank Yisrael, this has been super helpful.

However, i’m having a challenging with joining the multiple-references query with query of a regular field. I’m essentially using multiple dropdowns to perform a query. One of these dropdowns is a multiple reference for which i’ve used your suggestion (.hasSome), which works. Now i need to join this with the dropdown filter of the regular field in my database.

The problem is each of the dropdowns work separately. I’d like all the dropdowns to work together.

Here’s my code:

// Filter function for region, subCity & ownership dropdowns

function searchFilter() {
wixData.query(‘Hospitals’) .eq(‘region’, $w(" #dropdownRegion “).value) .and (wixData.query(“Hospitals”) .contains(“subCityZone”, $w(” #dropdownSubCity “).value)) .and (wixData.query(‘Hospitals’) .contains(“ownership”, $w(” #dropdownOwnership “).value))
.find()
.then(res => { $w(” #resultsTable2 ").rows = res.items;

let totalCount = res.items.length;
if (totalCount === 0) {
$w(’ #textCount ‘).text = “No result found!”;
}
else {
$w(’ #textCount ').text = ${totalCount} result found.;
}
})
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code; } ); }

Below is the multiple reference query.

//filter by radiology procedure dropdown
async function queryRadiology() {
let RadProcedure = $w(" #dropdownServices ").value;
let radiology = ( await wixData.query(‘Radiology’)
.eq(‘title’, RadProcedure) .find()).items[0];
let res = await wixData.query(‘Hospitals’)
.hasSome(‘radiology’, [radiology._id])
.find();
console.log(res.items);
$w(’ #resultsTable2 ‘).rows = res.items; let totalCount = res.items.length;
if (totalCount === 0) {
$w(’ #textCount ‘).text = “No result found!”;
} else {
$w(’ #textCount ').text = ${totalCount} result found.;
} }

So, how can join these two function or ensure the queries run at the same time if selected?