1

I run via two for-loops through a MySQL table "Book" to get the values:

If I use explicitly the column name "Betrag" to get the value (e.g. res[i].Betrag ), it works! When I use for the column name a variable ('colName'), it issues an error message.

How can I get the values of a MySQL table without typing the column name in the code?

Thanks for your help, M@trix

Value.toString()

app.get('/', function (req, res) {
  //Build connection to MySQL database
  var mysql2 = require('mysql');

  var db2 = mysql2.createPool({
  host: "127.0.0.1",
  user: "...",
  password: "...",
  database: "DataRobot"
  });

db2.query('SELECT * FROM Book', function (error, results, fields) {
  if (error) throw error;

});


function setResHtml(sql, cb){
    db2.getConnection((err, con)=>{
    if(err) throw err;

    con.query(sql, (err, res, cols)=>{

      if(err) throw err;
      for(var i=0; i<res.length; i++){
        for(var j=0; j<3; j++) {
          var colName= cols[j].name;
          console.log("res[i].Betrag:"); 
          console.log(res[i].Betrag); // shows the value 
          console.log("res[i].colName:");
      console.log(res[i].colName); // shows an error message!!!!
    };
      }
      con.release(); 
      return cb(table);
      });
    });
  }

Get entries via a variable for the column name.

  • What happens if you `console.log(res[i])`? Could you use a for-loop and iterate all the values of res[i]? – dustytrash Apr 02 '19 at 14:53
  • I think what you're trying to achieve should be written `res[i][colName]` instead of `res[i].colName`. – Stock Overflaw Apr 02 '19 at 17:28
  • @dustytrash: res[i] gives back a string including all column names and entries. However, as being a single string, nothing that you can use for iteration. – user11300277 Apr 02 '19 at 18:45
  • You should still be able to iterate over the entries. If it's an object [see here](https://stackoverflow.com/questions/921789/how-to-loop-through-a-plain-javascript-object-with-the-objects-as-members) – dustytrash Apr 02 '19 at 18:47
  • If (1) `res[i].Betrag` shows the value and (2) `colName === 'Betrag'` is `true`, then `res[i][colName]` _has to_ show the value. If not, either (1) or (2) is false. Besides, two caveats I see from here. 1/ you don't use only `'Betrag'`: you try 3 indexes of `cols`, so are you sure `cols` holds at least 3 values? 2/ your code shouldn't pop an error, it should only show `undefined` for `res[i].colName`, so what is the error, maybe it's unrelated? – Stock Overflaw Apr 02 '19 at 20:15
  • please see this https://stackoverflow.com/questions/19648258/how-to-list-column-in-mysql-on-nodejs-with-module-mysql here you will get to know how to list down all columns of table and then loop over the columns. – Vikash_Singh Apr 03 '19 at 09:12

1 Answers1

1

@Stock Overflaw: You are absolutely right. I tried res[i][Betrag]; that did not work. However, res[i][colName] works! Thank you very much!

As a summary:

You get a single entry of an array with variable (e.g. colName):

res[i][colName]

You get a single entry of an array without variable (e.g. "Betrag"):

res[i].Betrag