Need help with many-to-many collection query for repeater

Hi all,

WIX (and JavaScript) noob here. I have a site where I have 3 collections: Specialty (contains just a unique list of therapist specialties), Therapist (contains details about each therapist) and then a many-to-many collection called TherapistCollection that contains two reference columns (one pointing to Therapist and one pointing to Specialty). As you might gather, each therapist can have one or more specialties, and a specialty can be shared by multiple therapists, hence the need for a many-to-many collection.

I am using a dropdown menu where the site visitor will select a single specialty and, based on what they select, then a repeater will become visible showing just those therapists that have that specialty.

I’m almost there, but I am having issues getting the “results2.items” array in the code below into a variable that I can then pass in a query to the Therapists collection, so that I can filter the repeater to only show data for those therapists. I can do this easily enough for single values, but if I have multiple values (ie: therapists with that specialty), I am unsure what to do as when I try to assign it to the “secondItem” variable, it comes back as “undefined” in console.log…

Below is my code so far:

import wixData from "wix-data";

$w.onReady(function () {

});

let selectedConcern = ""; // initialize the value to NULL
let selectedTherapists = "";

export function dropdown1_change_1(event) {
 
    wixData.query('Specialty')
        .eq("title", $w("#dropdown1").value)
 //.include("therapist")
        .find() // Run the query
        .then(results1 => {
            console.log("Specialty chosen is:");
            console.log(results1.items);
 
 let firstItem = results1.items[0]
            selectedConcern = firstItem._id;

            wixData.query('TherapistSpecialty')
                .eq("specialty", selectedConcern)
                .find()
                .then(results2 => {
                    console.log("Therapists with the chosen specialty are:");
                    console.log(results2.items);
 let secondItem = results2.items;
                    selectedTherapists = secondItem;
                    console.log("Chosen therapists' .therapist IDs:");
                    console.log(selectedTherapists);

                    wixData.query("Therapists")
                        .eq("_id", selectedTherapists)
                        .find()
                        .then(result => {
                            console.log("Therapist details for specialty chosen:");
                            console.log(result.items);
                            $w("#repeater1").data = result.items;
                            $w("#repeater1").expand();
                        })

                })
                .catch((err) => {
 let errorMsg = err;

                });

        });

}

Any help would be greatly appreciated!

Thanks,

Ian

Anyone? Seems like this should be a pretty common scenario. Hoping somebody can help…

Did I stump the community with my question? I’m seeing other posts from people that were submitted 2 hours ago that already have 2 or more replies, so kinda wondering why mine has gone without a single answer. Starting to think my post is invisible…