【问题标题】:Promises with Node.js and MysqlNode.js 和 Mysql 的 Promise
【发布时间】:2017-11-16 10:09:26
【问题描述】:

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

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;
    });


}

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

但是传了参数,怎么组织呢?

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

var pr1 = 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;
      });

    var pr2 = 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;
      });

    var pr3 = 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;
      });

    var pr4 = 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;
      });

    var pr5 = 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;
      });

    var pr6 = 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;
      });

    var pr7 = 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;
      });

    Promise.all(pr1, pr2, pr3, pr4, pr5, pr6, pr7)
      .then(function(){
        console.log(struct);
      })
       .catch(function(err){
            throw err;
      });   
}

【问题讨论】:

  • 谢谢alwaysone,我会检查这个。看起来不错。
  • 查看我的编辑。
  • 并没有真正阅读过这个问题,但是整个 if-else 块可以简化为这样的...... struct['label${records[i].type}'] = records [i].type;

标签: javascript mysql node.js promise bluebird


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 2018-05-26
    • 2015-10-28
    • 1970-01-01
    • 2013-05-26
    • 2022-01-10
    • 2016-08-01
    相关资源
    最近更新 更多