Tuesday, 2 May 2017

NodeJS+Express App with sequential MySQL queries [duplicate]

This question already has an answer here:

Now learning JS programming. On client request server rendering Jade/Pug page with data as results of several SQL queries. So I have this code:

router.get('/something', function(request,response,next) {
  
  var summary = [];
  
  var sql1 = 'SELECT 1+1 AS Q1';
  var sql2 = 'SELECT 2+2 AS Q2';
  var sql3 = 'SELECT 3+3 AS Q3';
  
  pool.query(sql1, function (err,res) {
    if (err) throw err; else summary.push(res);
    pool.query(sql2, function (err,res) {
      if (err) throw err; else summary.push(res);
      pool.query(sql3, function (err,res) {
        if (err) throw err; else summary.push(res);
        response.render('res1', 
          { title: 'this is something', summary: summary }
        });
      });
  });
    
});

Do anybody have some ideas how make this code more beauty? If it's possible to make many SQL queries without building pyramid, then it will be great for me. And more: How execute sequential queries from array like this ['sql1','sql2',..,'sqlN'] with controllable performance on server, and response with results in one render?



via Artyom Tsybulkin

No comments:

Post a Comment