here is what i trying to achieve, i going to create array json like this :
[{
"KODE_CLAIM" : "MMKLKKK01",
"DETAILS" : {
"No_SVC" : "1233456789",
"Date_SVC" : "01-01-2016"
}
},
{
"KODE_CLAIM" : "MMKLKKK02",
"DETAILS" : {
"No_SVC" : "1233456789",
"Date_SVC" : "01-01-2016"
}
}]
that's a nested JSON, i get the data from SQL Server. Here is what i do :
exports.reportClaim = function(req, resp) {
var kode_bass = req.params.kode_bass
var tgl_Awal = req.params.tgl_Awal
var tgl_Akhir = req.params.tgl_Akhir
var hddt = []
var details = [];
console.log(kode_bass,tgl_Awal,tgl_Akhir);
db.executeSql("exec NG_CLAIM_REPORT_HD '" + kode_bass + "','" + tgl_Awal + "','" + tgl_Akhir + "'" , function(data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
} else {
for(var i=0;i < data.length; i++){
console.log(data[i].KODE_CLAIM);
hddt.push(data[i].KODE_CLAIM)
db.executeSql("exec NG_CLAIM_REPORT_DT '" + data[i].KODE_CLAIM + "'" , function(data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
} else {
for(var i=0;i < data.length; i++){
console.log(data.length);
hddt[details] = data;
console.log(hddt);
}
};
});
}
httpMsgs.sendJson(req, resp, hddt);
};
});
};
i facing two problems, first i cannot push the data into hddt[] array, and the second i think the callback cause that, the JSON already send to webpage, but the for looping is late, i think it because callback right?
when i do the second loop in here :
db.executeSql("exec NG_CLAIM_REPORT_DT '" + data[i].KODE_CLAIM + "'" , function(data, err) {
if (err) {
httpMsgs.show500(req, resp, err);
} else {
for(var i=0;i < data.length; i++){
console.log(data.length);
hddt[details] = data;
}
};
});
the error in console said, "details" is not defined How to i fix that? i there another way to exec SQL Server without callback? and how to push my second stored procedure data into my array object and return it as JSON?
Below is my SQL data when execute "NG_CLAIM_REPORT_HD" KODE_CLAIM 1 CLM/B094/1403/0001 2 CLM/B094/1403/0002
here is my SQL data when execute "NG_CLAIM_REPORT_DT" with param "CLM/B094/1403/0001"
KODE_CLAIM No_SVC Date_SVC
1. CLM/B094/1403/0001 SVC/B094/1401/0026 2014-01-20 00:00:00.000
2. CLM/B094/1403/0001 SVC/B094/1309/0003 2013-09-18 00:00:00.000
When i execute SP NG_CLAIM_REPORT_DT with param "CLM/B094/1403/0002"
KODE_CLAIM No_SVC Date_SVC
1. CLM/B094/1403/0002 SVC/B094/1312/0006 2013-12-16 00:00:00.000
2. CLM/B094/1403/0002 SVC/B094/1312/0005 2013-12-16 00:00:00.000
so the JSON data i want is :
[{
"KODE_CLAIM" : "CLM/B094/1403/0001",
"DETAILS" : [
{
"No_SVC" : "SVC/B094/1401/0026",
"Tgl_SVC" : "2014-01-20 00:00:00.000"
},
{
"No_SVC" : "SVC/B094/1309/0003",
"Tgl_SVC" : "2013-09-18 00:00:00.000"
}
]
},
{
"KODE_CLAIM" : "CLM/B094/1403/0002",
"DETAILS" : [{
"No_SVC" : "SVC/B094/1312/0006",
"Tgl_SVC" : "2014-01-20 00:00:00.000"
},
{
"No_SVC" : "SVC/B094/1312/0005",
"Tgl_SVC" : "2013-09-18 00:00:00.000"
}
]
}]
i cannot push the data into hddt[] arraywhy not? do you get an error on thehddt.push(data[i].KODE_CLAIM)line?detailsdefineddetailsvariable, you have anivariable inside for loop. Also u have the first problem cause you dont havedetailsi believe.