1

Maybe I've been staring at my code too long or maybe I'm using too many callbacks elsewhere or something, but I can't figure out the logistics of this one. I know the value contained in rows is correct from using util.inspect(rows) on it inside that callback function. I just don't know how to bring it back up to the surface, so to speak. dbHandler() is called from other functions that pass the params and a result is expected in return.

Any insight would be tremendously helpful.

Using node.js with Express and mysql packages if that makes a difference.

function dbHandler(req, res, params) {
    pool_user.getConnection(function (err, connection) {
        if (err) {
            connection.release();
            res.json({
                "code"   : 100,
                "status" : "Error in connection database"
            });
            return;
        }
        connection.query(params.query_string, function (err, rows) {
            connection.release();
            if(!err) {
                res.json(rows);  // <<--- I want to get ahold of the value of `rows`
                //console.log(util.inspect(rows));
            }           
        });
        connection.on('error', function(err) {
            res.json({
                "code"   : 100,
                "status" : "Error in connection database"
            });
            return;
        });
    });
}
jdstankosky
  • 657
  • 3
  • 15
  • 3
    Asynchronous. You're accessing the global before the callback completes. – Dave Newton Jun 19 '15 at 00:55
  • Where are you accessing `umadbro`? If you don't show us that part of the code where `umadbro` is unexpectedly `undefined`, we cannot help you with fixing it. – Bergi Jun 19 '15 at 01:23
  • @Bergi Sorry, I was making incremental post edits. :P I'm not using `umadbro`. My question was confused (my fault) for something else. – jdstankosky Jun 19 '15 at 01:25
  • 1
    OK whatever then to the current question: `res.json` seems to be quite good at "bringing the value back to the surface", isn't it? If those other functions that call `dbHandler`, then they should just supply their own callbacks that would then need to be called instead of `res.json`. – Bergi Jun 19 '15 at 01:38
  • possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – Paul Jun 19 '15 at 04:58

1 Answers1

0

Trying to return the result of an async function is an anti-pattern and will never work.

Instead, restructure the calling code.

Not Like This - the anonymous callback does not return to the caller

new_data = api_request_data(auth, request, 
      function(error, data){ 
          return data;
});
store(new_data);
update_screen(new_data);

Better

api_request_data(auth, request, 
       function(error, data){ 
          store(data);
          update_screen(data);
});

Best

api_request_data(auth, request, 
       function(error, data){
          if (error){ 
                console.log(error);
                return;
          } 
          store(data);
          update_screen(data);
});

In your code, the data pipeline looks ok.

connection.query(params.query_string, function (err, rows) {
        connection.release();
        if(!err) {
            res.json(rows);  // <<--- I want to get ahold of the value of `rows`
            //console.log(util.inspect(rows));
        }           
    });

The code res.json(rows); in a node.js application will queue a JSON string containing the data in rows for writing to the current web connection referenced in res.

Your question suggests that the issue is that this line does not return data to the caller of dbHandler(), but no trained node.js developer would expect that to happen. The code looks correct for delivering the data in rows to an over-the-web client of the node.js application.

Paul
  • 26,170
  • 12
  • 85
  • 119