使用Node.js和Mysql的Promise

时间:2017-06-14 10:08:36

标签: javascript mysql node.js promise bluebird

我有一个函数,它在参数中有一个结构struct,这个结构由一些异步SQL请求设置,如下面的代码所示:

this

但是,在执行所有MySQL请求之后,我想做一个超级承诺(为此我使用bluebird.js)这样(http://bluebirdjs.com/docs/api/new-promise.html)以返回设置的结构。

但是传递了参数,如何组织它?

我尝试使用Promise.all,但它不起作用。

module.exports.loadDatas = function(struct) {

    db.queryAsync('SELECT * FROM safeplusdb.vector;')
    .then(function(records){
        for(i=0; i<records.length; i++)
        {
          if(records[i].type=="ADSL")   
          {
            struct.labelAdsl=records[i].type;
          }
          else if (records[i].type=="Cable")
          {
            struct.labelCable=records[i].type;
          }
          else if (records[i].type=="Fibre")
          {
            struct.labelFibre=records[i].type;
          }
          else if (records[i].type=="Others")
          {
            struct.labelOthers=records[i].type;
          }
          else if (records[i].type=="Ott")
          {
            struct.labelOtt=records[i].type;
          }
          else if (records[i].type=="Satellite")
          {
            struct.labelSatellite=records[i].type;
          }
          else if (records[i].type=="Tnt")
          {
            struct.labelTnt=records[i].type;
          }
        }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.data;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        let record= records[i];

        if(record.vector_id==0)
        {
          factorizeSQL(UTCGap, struct.cllForecastAdsl, struct.valueReturnAdsl, record);       
        }

        else if(record.vector_id==1)
        {
          factorizeSQL(UTCGap, struct.cllForecastCable, struct.valueReturnCable, record);        
        }

        else if(record.vector_id==2)
        {
          factorizeSQL(UTCGap, struct.cllForecastFibre, struct.valueReturnFibre, record);
        }

        else if(record.vector_id==3)
        {
          factorizeSQL(UTCGap, struct.cllForecastOthers, struct.valueReturnOthers, record);
        }

        else if(record.vector_id==4)
        {
          factorizeSQL(UTCGap, struct.cllForecastOtt, struct.valueReturnOtt, record);
        }

        else if(record.vector_id==5)
        {
          factorizeSQL(UTCGap, struct.cllForecastSatellite, struct.valueReturnSatellite, record);
        }

        else if(record.vector_id==6)
        {
          factorizeSQL(UTCGap, struct.cllForecastTnt, struct.valueReturnTnt, record);
        }
      }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.admin;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        struct.adminList[i]=records[i].name;
      }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.media;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        struct.mediaList[i] = { label : records[i].label, value : records[i].value};
      }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.scroll ORDER BY orderscroll;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        let record= records[i];

        if(record.isscrolled==false)
        {
          if(record.idm==null && record.vector_id !=null)
          {
            struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
            struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
            struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id]; 
            struct.indj++;
          }
          else if(record.vector_id==null && record.idm!=null)
          {
            struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
            struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
            struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
            struct.indj++;
          }
          else
          {
            struct.listUnScroll[struct.indj] = {media : null, platform : null, orderscroll: null};
            struct.listUnScroll[struct.indj].orderscroll = record.orderscroll;
            struct.listUnScroll[struct.indj].media = struct.listMedias[record.idm-1];
            struct.listUnScroll[struct.indj].platform = struct.listPlatforms[record.vector_id]; 
            struct.indj++;  
          }    
        }

        else if(record.isscrolled==true)
        {
          if(record.idm==null && record.vector_id !=null)
          {
            struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
            struct.listScroll[struct.indk].orderscroll = record.orderscroll;
            struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id]; 
            struct.indk++;
          }
          else if(record.vector_id==null && record.idm!=null)
          {
            struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
            struct.listScroll[struct.indk].orderscroll = record.orderscroll;
            struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
            struct.indk++;
          }
          else
          {
            struct.listScroll[struct.indk] = {media : null, platform : null, orderscroll: null};
            struct.listScroll[struct.indk].orderscroll = record.orderscroll;
            struct.listScroll[struct.indk].media = struct.listMedias[record.idm-1];
            struct.listScroll[struct.indk].platform = struct.listPlatforms[record.vector_id]; 
            struct.indk++;  
          }       
        }
      }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.device;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        struct.deviceList[i] = {value : records[i].value,  label : records[i].label};
      }
    })
    .catch(function(err){
      throw err;
    });

  db.queryAsync("SELECT * FROM safeplusdb.listmsd;")
    .then(function (records){
      for(i=0; i<records.length; i++)
      {
        let aaa = records[i].namelist;
        let ccc = [];
        struct.msdList[i] = {id: records[i].idlistmsd, labelList: aaa, msds : []};  
      }

      let k = struct.msdList.length;

      db.queryAsync("SELECT * FROM safeplusdb.msd;")
        .then(function (records1)
        {
          for(j=0; j<records1.length; j++)
          {
            for(l=0;l<k;l++)
            {
              if(records1[j].listmsd==struct.msdList[l].id)
              {
                struct.msdList[l].msds.push(records1[j].msd);
              }
            }
          }
        })
        .catch(function(err){
          throw err;
        });         
    })
    .catch(function(err){
      throw err;
    });


}

1 个答案:

答案 0 :(得分:0)

首先,您的loadDatas函数必须是异步的 - 这是在其中使用异步调用的结果。在当前的实现中,当所有异步调用完成且您的结构充满数据时,您将无法解决。

你应该考虑链接Promises。看一下这篇文章(只是忽略Angular的东西):http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/