如何简化节点js查询中的嵌套if循环

时间:2018-07-20 10:59:46

标签: mysql node.js

我需要简化以下嵌套的if循环代码。如何在Node JS中做 我还需要在jscript本身而不是在ejs中进行基于用户的查询调用。如何使用节点js在图表中显示表格数据。 morris js图表是否与节点js兼容。如果是这样,如何将数据从渲染函数的节点js对象解析为json。

var moment = require('moment');
var currencyFormatter = require('currency-formatter');
var db = require('./db.js');

const siteTitle = "SalesFlow | Luftek";
const mainURL = "http://sales.luftek.in/";

var dquery = "SELECT COUNT(*) as pending FROM enquiries WHERE status = 'Processing';";
var listdquery = "SELECT * FROM enquiries WHERE status = 'Processing';";
var listsquery = "SELECT * FROM salesorder_b WHERE po_date >= NOW()- INTERVAL 14 DAY;";
var dsquery = "SELECT count(*) as recent_order FROM salesorder_b WHERE po_date >= NOW()- INTERVAL 14 DAY";
var hitratio = "SELECT ((SELECT (SELECT count(*) FROM enquiries WHERE job_ref = (SELECT job_ref FROM salesorder_b where enquiries.job_ref = salesorder_b.job_ref) ) / (SELECT count(*) FROM enquiries))) * 100 as hitratio;";
var hitratioazeem = "SELECT ((SELECT (SELECT count(*) FROM enquiries WHERE job_ref = (SELECT job_ref FROM salesorder_b where enquiries.job_ref = salesorder_b.job_ref) & sales_per = 'Azeem' ) / (SELECT count(*) FROM enquiries where sales_per = 'Azeem'))) * 100 as hitratioazeem;";
var hitratiosuresh = "SELECT ((SELECT (SELECT count(*) FROM enquiries WHERE job_ref = (SELECT job_ref FROM salesorder_b where enquiries.job_ref = salesorder_b.job_ref) & sales_per = 'Suresh M' ) / (SELECT count(*) FROM enquiries where sales_per = 'Suresh M'))) * 100 as hitratiosuresh;";
var azeempendingenquiry = "SELECT COUNT(*) as pending FROM enquiries WHERE status = 'Processing' && sales_per = 'Azeem';";
var hitratiochart = "SELECT * FROM hitratio;";

exports.dboard = function (req, res) {

  if (req.user) {
    db.query(hitratio, function (err, hitresult) {
      if (err) { throw err; }
      else {
        db.query(dsquery, function (err, result1) {
          if (err) { throw err; }
          else {
            db.query(listdquery, function (err, plistresult) {
              if (err) { throw err; }
              else {
                db.query(listsquery, function (err, olistresult) {
                  if (err) { throw err; }
                  else {
                    db.query(dquery, function (err, result) {                
                      if (err) throw err;
                      else {
                        db.query(hitratioazeem, function (err, hitresultazeem) {
                          if (err) throw err;
                          else{
                            db.query(hitratiosuresh, function (err, hitresultsuresh) {
                              if (err) throw err;
                              else{
                                db.query(hitratiochart, function (err, hitresultchart) {
                                  if (err) throw err;
                                  else{
                                    res.render('pages/dashboard', {
                                      siteTitle: siteTitle,
                                      moment: moment,
                                      currencyFormatter: currencyFormatter,
                                      pageTitle: "Dashboard",
                                      hitresult1: hitresult[0],
                                      hitresult2: hitresultazeem[0],
                                      hitresult3: hitresultsuresh[0],
                                      sales: result1[0].recent_order,
                                      items: result[0].pending,
                                      hititems : hitresultchart,
                                      plists: plistresult,
                                      olists: olistresult,
                                      user: req.user
                                    });
                                  }
                                });
                              }
                            });
                          } 
                        });
                      }
                    })
                  }
                });
              }
            });
          }
        });
      }
    });
  }
  else { res.redirect(mainURL); }

}

1 个答案:

答案 0 :(得分:0)

“嵌套的if循环”通常称为“回调地狱”,并且有各种实用程序包可以处理此问题。一种流行的方法是async,它为此特定用例提供了async.parallel

async.parallel([
  (callback) => db.query(query1, callback),
  (callback) => db.query(query2, callback), 
  (callback) => db.query(query3, callback), 
], (err, results) => {
  if (err) throw err;

  res.render('pages/dashboard', {
    data1: results[0],
    data2: results[1],
    data3: results[2],
  }) 
})

要“呈现” JSON,请签出Express'res.json

使用“基于用户的查询调用”,我不太了解您所指的内容。如果您正在谈论动态构建SQL查询,则建议使用诸如knex之类的软件包。