2017-06-14 104 views
0

我有一个函数,该函数具有在参数的结构struct和这个结构是由一些异步SQL请求设置好的,如在下面的代码:承诺与Node.js的和MySQL

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


} 

然而,我在执行完所有的MySQL请求之后,为了返回设置的结构,我想使用(http://bluebirdjs.com/docs/api/new-promise.html)作出超级承诺(为此我使用bluebird.js)。

但随着参数的传递,如何组织呢?

我尝试使用Promise.all,但它不起作用。所有loadDatas功能

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

看看这个:http://bluebirdjs.com/docs/api/promise.all.html – oneturkmen

+0

感谢alwaysone,我会检查这一点。这似乎很好。 –

+0

看看我的编辑。 –

回答