Javascript Node.js和Mysql的承诺
我有一个函数,在参数中有一个结构struct,这个结构是由一些异步SQL请求设置的,如下代码所示:Javascript Node.js和Mysql的承诺,javascript,mysql,node.js,promise,bluebird,Javascript,Mysql,Node.js,Promise,Bluebird,我有一个函数,在参数中有一个结构struct,这个结构是由一些异步SQL请求设置的,如下代码所示: module.exports.loadDatas = function(struct) { db.queryAsync('SELECT * FROM safeplusdb.vector;') .then(function(records){ for(i=0; i<records.length; i++) { if(reco
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;
});
}
module.exports.loadDatas=函数(结构){
db.queryAsync('SELECT*FROM safeplusdb.vector;')
.然后(功能(记录){
对于(i=0;i首先,您的loadDatas
函数必须是异步的-这是在函数中使用异步调用的结果。在您当前的实现中,当所有异步调用完成并且您的结构充满数据时,您将无法取消理解
你应该考虑链接的承诺。看一看这篇文章(忽略无关紧要的东西):
检查一下:谢谢,一个,我会检查一下。它看起来不错。看看我的编辑。实际上没有读过这个问题,但是整个IF块可以被简化成这样的…结构['LabVIE{{Real[i],类型} ] =记录[i]。
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;
});
}