# Populating Dropdown Using Reference Field

**URL:** <https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [March 15, 2018, 1:55am UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583 "2018-03-15T01:55:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael-2](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@michael-2](https://forum.wixstudio.com/u/michael-2)\
**Post date:** [March 15, 2018, 1:55am UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/1 "2018-03-15T01:55:07Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![michael-2](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@michael-2](https://forum.wixstudio.com/u/michael-2)\
**Post date:** [March 15, 2018, 3:02am UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/2 "2018-03-15T03:02:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alessuae](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@alessuae](https://forum.wixstudio.com/u/alessuae)\
**Post date:** [May 4, 2020, 1:19pm UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/3 "2020-05-04T13:19:48Z")

</div>

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.

 ![](https://us1.discourse-cdn.com/wix/original/3X/d/1/d1211968568116787060665a981d0941eee1b357.jpeg)

 ![](https://us1.discourse-cdn.com/wix/original/3X/7/f/7f3aad2014fb5acda64e5a023db60b8fa1aaa32a.jpeg)

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [May 4, 2020, 1:48pm UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/4 "2020-05-04T13:48:13Z")

</div>

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

 ![](https://us1.discourse-cdn.com/wix/original/3X/1/b/1b40910ae3e37f44c23e0cd41cd9d19e25331b0f.png)

---

<div class="post-metadata">

**Author:** ![alessuae](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@alessuae](https://forum.wixstudio.com/u/alessuae)\
**Post date:** [May 4, 2020, 1:52pm UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/5 "2020-05-04T13:52:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [May 4, 2020, 1:57pm UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/6 "2020-05-04T13:57:08Z")

</div>

@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](https://www.wix.com/corvid/forum/main/comment/5eb01a313124c300187ce3f5)](you?  
[https://www.wix.com/corvid/forum/main/comment/5eb01a313124c300187ce3f5](https://www.wix.com/corvid/forum/main/comment/5eb01a313124c300187ce3f5)  
The)

---

<div class="post-metadata">

**Author:** ![volkaertskristof](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/volkaertskristof/32/18158_2.png) [@volkaertskristof](https://forum.wixstudio.com/u/volkaertskristof)\
**Post date:** [May 4, 2020, 2:00pm UTC](https://forum.wixstudio.com/t/populating-dropdown-using-reference-field/25583/7 "2020-05-04T14:00:18Z")

</div>

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 😅)
