I'm trying to create a dropdown list of dates from firebase data. But the list has no options and I'm getting no errors in the console. Here's the code:
<script>
var dates = [];
$.getJSON("https://earthquake-detection-default-rtdb.firebaseio.com/X-Axis.json", function(data){
    $.each(data, function(key, value){
        if (dates.indexOf(value.date) === -1) {
                    dates.push(value.date);
            }
    })
});
console.log(dates);
var sel = document.getElementById("arr");
for(var d=0; d<dates.length; d++) {
        var dateElem = document.createElement("option");
        dateElem.value = dates[d];
        dateElem.textContent = dates[d];
        sel.appendChild(dateElem);
    } 
</script>
I'm getting the output if I do this:
<script>
var dates = ["2021-03-06","2021-04-05"];
console.log(dates);
var sel = document.getElementById("arr");
for(var d=0; d<dates.length; d++) {
        var dateElem = document.createElement("option");
        dateElem.value = dates[d];
        dateElem.textContent = dates[d];
        sel.appendChild(dateElem);
    } 
</script>
I don't understand what I'm doing wrong.
 
    