Populating Dropdown Using Reference Field

Apologies in advance if this a subject that has been gone over repeatedly. I’ve gone through a lot of material provided for how to load dropdowns via code. I think I’m heading in the right direction but can’t quite get what I want to work.

I have two collections, Countries and Cities, that I want to populate on a search page via dropdowns. Cities contains a reference field to Countries. The city dropdown should only enable once a country is selected.

Here is the code:
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixData from ‘wix-data’;

$w.onReady(() => {
//Set up the Country dropdown
wixData.query(‘Countries’)
.ascending(“title”)
.find()
.then(res => {
let options = [{
“value”: ‘’,
‘label’: ‘Select Country’
}];
options.push(…res.items.map(country => {
return {
‘value’: country._id,
‘label’: country.title
};
}));
$w(‘#countryDD’).options = options;
});
});

export function countryDD_change(event, $w) {
//Set up the City dropdown based on the selection made in the Country dropdown
console.log($w(‘#countryDD’).value);
wixData.query(‘Cities’)
.eq(“Country”, $w(‘#countryDD’).value)
.ascending(“title”)
.find()
.then(res => {
let options = [{
“value”: ‘’,
‘label’: ‘Select City’
}];
options.push(…res.items.map(city => {
return {
‘value’: city._id,
‘label’: city.title
};
}));
$w(‘#cityDD’).enable();
$w(‘#cityDD’).options = options;
});
}

The first dropdown seems to work fine. The console log shows the actual key (_id) of the country selected. Unfortunately, the City dropdown is not populated at all (except for the “Select City” label). This leads me to believe that there are no records being returned from the query.

Is there something obvious I’m missing?

Figured it out. When creating the .eq condition in the query I was referencing the Field Name (Country) instead of the Field Key (country).

Lesson learned on naming conventions I guess.

Hi Michael, can you help me on this pls? I was able to retrieve the items but it gives me [object undefined] for the options.

@alessuae
since ou get the items, it means Cities has been succesfully accessed.
so the problem probably lies at the category.country.

is the value of the country field right?
When you misspeled once the value name doesn’t change when changing the label.
(i had this problem a few times)

made an example :
Got a row named Country, but first i named it countryy and removed a ‘y’
after chaning the label name the value name is still the same.

@volkaertskristof hi, thanks for replying that quick.
By the way yes the field is typed correctly. The fieldkey is “country”

I can see the items ID but can’t convert them into a String, I used toString JSON.stringify as well…
But nothing…

@alessuae
You’re welcome,
i can’t see the problem why it should’t work but maybe this answer can help you?

[https://www.wix.com/corvid/forum/main/comment/5eb01a313124c300187ce3f5](you?
https://www.wix.com/corvid/forum/main/comment/5eb01a313124c300187ce3f5
The)

If that code doesn’t help,
can you take a screenshot of your database?
maybe i see something you don’t se (4 eye’s are better then 2 :sweat_smile:)