5

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?

Pieter van den Ham
  • 4,381
  • 3
  • 26
  • 41
Barry Smith
  • 105
  • 1
  • 2
  • 10
  • 2
    I believe [this](https://stackoverflow.com/questions/14379274/javascript-iterate-object?noredirect=1&lq=1) is your answer. – Ishaan Shakunt Jun 07 '17 at 09:59

4 Answers4

7

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>";
  }
}
Yosef Tukachinsky
  • 5,570
  • 1
  • 13
  • 29
Datsik
  • 14,453
  • 14
  • 80
  • 121
  • You are on the right track ... I just need to know how to get the value of an obj ... Since I won't know the field names ... Googling now .... – Barry Smith Feb 28 '16 at 12:05
3

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);
});
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Mohd Arshil
  • 195
  • 1
  • 1
  • 9
2

I used below snippet to extract the data by iterating the RowDataPacket after successfully fetching the results from MySQL Query


if(error) {

    }
    else{
            Object.keys(results).forEach(function(key) {
            var result = results[key];      
            console.log(result.title,result.body,result.id);   
    });
}
SPS
  • 31
  • 3
1

I am not sure if Mohd's code would work but I did get the following code to work for me:

For my column headers ...

theShit = "<TR>";
  for (var i = 0; i < fields.length; i++) {
    console.log(fields[i].name);
    theShit = theShit + ("<TH>" + fields[i].name + "</TH>");
  }

... and for each row ...

for (var i = 0; i < rows.length; i++) {
    theShit = "<TR>";
    // hardcoded way -> theShit = theShit + ('| ' + rows[i].name + ' | ' +  rows[i].address_zip);
    myObj = rows[i];
    for( var j in myObj ) {
      if (myObj.hasOwnProperty(j)) {
        theShit = theShit + ("<TD>" + myObj[j] + "</TD>");
      }
    }
Barry Smith
  • 105
  • 1
  • 2
  • 10