I am attempting to populate drop down list dynamically. The list contains of states that are available in database . I am not supposed to add all 50 states, just the ones in the database , hence why this has to be dynamic.
This is not working because there is no 'add' function available to 'dropdown' element. Here is the error:
Project-fields.js:104 Uncaught (in promise) TypeError: e.add is not a function
The error is pointing to the line 'dropdown.add(options);'
This is the JSON object I receive upon fetch():
STATES: [{"_id":"Virginia","name":"Virginia","abbreviation":"VN","__v":0},{"_id":"North Carolina","name":"North Carolina","abbreviation":"NC","__v":0},{"_id":"Texas","name":"Texas","abbreviation":"TX","__v":0},{"_id":"Louisiana","name":"Louisiana","abbreviation":"LA","__v":0},{"_id":"5f1ef364691bf8c340104c18","name":"California","abbreviation":"CA"}
My Code :
function StateSelect({ values, initial }) {
async function fetchStates(){
let dropdown = document.getElementById('select');
dropdown.length = 0;
.then(
function(response){
if(response.status !== 200)
{
console.warn('State Object Not Fetched' + response.status);
return;
}
response.json().then(function(data){
console.log(`STATES: ${JSON.stringify(data)}`);
for(let i = 0; i < data.length; i++)
{
options = document.createElement('option');
options.value = data[i].name;
options.label = data[i].name;
// setOptions(data[i].name);
dropdown.add(options);
console.log(`OPTION: ${options}`);
}
})
}
)
}
useEffect(() => {
fetchStates();
},[]);
return (
<div className='attribute-edit-container'>
<div className='attribute-name'>{'State'}</div>
<Select id="select">
</Select>
);
} //StateSelect
Please help. Thank you