javascript - How can I structure these callbacks to get me the info I want? -


maybe i've been staring @ code long or maybe i'm using many callbacks elsewhere or something, can't figure out logistics of one. know value contained in rows correct using util.inspect(rows) on inside callback function. don't know how bring surface, speak. dbhandler() called other functions pass params , result expected in return.

any insight tremendously helpful.

using node.js express , mysql packages if makes 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);  // <<--- want ahold of value of `rows`                 //console.log(util.inspect(rows));             }                    });         connection.on('error', function(err) {             res.json({                 "code"   : 100,                 "status" : "error in connection database"             });             return;         });     }); } 

trying return result of async function anti-pattern , never work.

instead, restructure calling code.

not - anonymous callback not return 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 code, data pipeline looks ok.

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

the code res.json(rows); in node.js application queue json string containing data in rows writing current web connection referenced in res.

your question suggests issue line not return data caller of dbhandler(), no trained node.js developer expect happen. code looks correct delivering data in rows over-the-web client of node.js application.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -