Pool Connection doubt.

71 views
Skip to first unread message

Luigi Padovani

unread,
Dec 11, 2014, 8:26:45 AM12/11/14
to node-...@googlegroups.com
Hi People,

i using this function to get a generic Param from DB

function getData(param)
{
var queryString = 'SELECT value FROM data where config like "%'+param+'%"';
  pool.getConnection(function(err, connection){

if (err) {
    console.log(err);
    throw err;
   }
  connection.query(queryString, function(err, rows, fields) {
   if (err) {
    console.log(err);
    throw err;
   }
return rows[0].value;
 });
});
}


but when i use this function :

function setData()
{
timeoutEstrazione = getData('countdown')*1000;
timeoutBall = getData('balltime')*1000;
timeoutPause = getData('pausetime')*1000;
console.log('timeoutEstrazione:',timeoutEstrazione);
}
the console output timeoutEstrazione:NaN

the field was filled later, the Connection Pool was async respect the code flow.

anyone can suggest me the correct way to fix this problem?


thx a lot.

Luigi


Ryan Lee

unread,
Dec 11, 2014, 1:02:36 PM12/11/14
to node-...@googlegroups.com
You have flow control issues. Remember javascript is asynchronous. getData returns undefined before your mysql query completes.

You can either us the async library, or a promise library to help you do flow control. 

var async = require('async');

//common callback convention is too pass in parameters and a callback function
//callback function has the signuature (error, values)
//if error is set you need to handle it.
var getData = function (param, next) {

    //escape your query. You don't want a sql injection
    pool.query('SELECT value FROM data where config like %?%', param, function (err, rows) {
       //return here does nothing. It is to break execution if there is an error.
       if (err) { return next(err); }

       //process your data here or just return it.
       //notice the null means no error
       next(null, rows[0].value);
    });
};

//chose series, parallel, or waterfall depending on what you need to do
async.parallel([
  function (cb) {
    getData('countdown', function (err, data) {
       cb(err, data * 1000);
    });
  },
  function (cb) {
    getData('balltime', function (err, data) {
       cb(err, data * 1000);
    });
  },
  function (cb) {
    getData('pausetime', function (err, data) {
       cb(err, data * 1000);
    })
  }
], function (err, results) {
  if (err) { throw err; //handle error}
  //this is timeoutEstrazione.
  console.log(results[0]);
});

This is a quick and dirty example, but your issues come from flow control. Javascirpt does not return things the way you think it will.

Good luck.

--
You received this message because you are subscribed to the Google Groups "node-mysql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+...@googlegroups.com.
To post to this group, send email to node-...@googlegroups.com.
Visit this group at http://groups.google.com/group/node-mysql.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages