callback problem.

10 views
Skip to first unread message

Buddha Priya Mowrya Kanneganti

unread,
Jul 28, 2017, 2:25:42 PM7/28/17
to LoopbackJS
Hi,
I'm new to Loopback, in below remotemethod foreach is getting executed by calling other queries(request.count) and it is returning method call without waiting for request.count to return value.

each time method is returning different different count. 
=========================================================
Scheme.WardWiseAppliedApproved = function (cb) {
var mWard = server.models.Ward;
var Request = server.models.Request;
mWard.find({"where":{"status": "Active"}}, function (err,wardList) {
var finalObject=[];
if(wardList!=undefined && wardList.length>0){
var tfalseCount=0;
var tapprovedCount=0;
var trejectedCount=0;
var totalCount=0;
var count=0;
wardList.forEach(function(ward) {
console.log(JSON.stringify(ward.name));
var ArgData = "'wardNumber':"+ward.name;
//console.log('--------------'+ArgData);
var queryData = {'wardNumber':ward.name};
Request.count({'and':[queryData]}, function (err, reports1) {
console.log('Total Applied'+JSON.stringify(reports1));
tfalseCount=reports1;
Request.count({'and':[queryData,{"finalStatus" : 'Approved'}]}, function (err, approved1) {
console.log('Total Approved'+JSON.stringify(approved1));
tapprovedCount=approved1;
Request.count({'and':[queryData,{"finalStatus" : 'Rejected'}]}, function (err, reject) {
trejectedCount=reject;
console.log('Total Rejected'+JSON.stringify(reject));
totalCount=tfalseCount+tapprovedCount+trejectedCount;

});});});
var data={
'schmeDetails':ward,
'tfalseCount':tfalseCount,
'tapprovedCount':tapprovedCount,
'trejectedCount':trejectedCount,
'totalCount':totalCount
}
finalObject.push(data);
count++;
console.log("-----done---::"+count);
if(wardList.length==count){
cb(null,finalObject);}

});
}
});
};
Reply all
Reply to author
Forward
0 new messages