Issue with for loop to add items to select box


#1

Hi there, I am having this issue:
Trying to fill select box from query and cannot get items to iterate into dropbox.
Here the code:
// Perform a query
$query = ‘SELECT nickname FROM names’;

connection.query($query, function(err, rows, fields) {
if(err){
console.log(“An error ocurred performing the query.”);
console.log(err);
return;
}
var x = document.getElementById(“mySlct”);
var option = document.createElement(“option”);

for (var i in rows) {
  option.text = rows[i].nickname;  
  x.add(option);
  }

});

// Close the connection
connection.end(function(){
// The connection has been closed
});

Code only puts one item (the last one) into select tag. I’ve tried a bunch of times to change the code to get it to work. No luck. Apparently, I need to study up on loop statements but there is not much on the web pertaining to mysql in Electron. Any help would be appreciated. Thanks
Tom


#2

The for...in statement is used to iterate over Objects that aren’t Array-like.
I don’t know what kind of object rows is but I would see if for...of works.
for...of goes over elements like a numerical for loop.


#3

Thank you, I’ll give that a try.