I'm a relative node newbie (old school programmer)
I have working code to grab a result set and parse through rows. I'm using felx's node-mysql driver. It's not hard to specifically print out columns.
connection.query('SELECT * FROM Company', function(err, rows, fields) {
if (err) throw err;
for (var i = 0; i < rows.length; i++) {
result = "";
result = result + ('| ' + rows[i].name + ' | ' + rows[i].address_zip);
theEmail = theEmail + result + "<BR>";
}
What I really want to do is loop through fields without having to name them ... my gut tells me it would be rows[i].[j]
... or something like that.
Is this possible?
If I understand your question correctly (you don't want to have to rows[i].name every time)
You can use For...of assuming all my mysql arrays are objects.
So you can do something like:
connection.query('SELECT * FROM Company', function(err, rows, fields) {
if (err) throw err;
for (var i = 0; i < rows.length; i++) {
for (obj of rows[i]) {
//obj = whatever field its currently on (name, email, w/e)
theShit = "";
theShit += ('| ' + rows[i].name + ' | ' + rows[i].address_zip);
theEmail += theShit + "<BR>";
}
}
I was able to solve the same by the following code.
Object.keys(result.rows[index]).forEach(function(key) {
var val = result.rows[index][key];
console.log('key is: ' + key);
console.log('val is: ' + val);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With