I understand the theory behind the parse promises(.then, .done, .when, etc.) but I dont know how to execute them. For now I am using alerts to force the system to wait long enough to fulfill the promises, but thats a really crude way of doing it. The code I will post does exactly what I want it to do. It creates a table, gathers info from the database, and formats it into the table. If i take out alert("forces a wait"); it wont work though, as the promise hasnt been fulfilled yet. Where and what should i add some of the promise handlers to make it work without the alert? I tried changing the for loop to a do while, and added .then but i couldnt get it to not throw errors, but im pretty sure thats because i formatted the structure wrong. Any help would be greatly appreciated. Thanks!
function billingReport(){
var sDate = new Date(document.getElementById("startDate").value);
var table = document.getElementById("results1");
var row, cell1, cell2, cell3, cell4;
var tableHeaderRowCount = 1;
var rowCount = table.rows.length;
for (var i = tableHeaderRowCount; i < rowCount; i++) {
table.deleteRow(tableHeaderRowCount);
}
Parse.Cloud.run("runReport", {sDate: sDate}, {
success: function(result){
alert("Successfully retrieved " + result.length + " scores.");
for(var i = 0; i < result.length; i++){
alert("forces a wait");
Parse.Cloud.run("caseHelper", {id: result[i].attributes.customer.id, className: "User", attribute: "username"},{
success: function(results){
row = table.insertRow(i);
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);
cell4 = row.insertCell(3);
cell1.innerHTML = result[i-1].id;
cell2.innerHTML = result[i-1].attributes.title;
cell3.innerHTML = result[i-1].attributes.hoursWorked;
cell4.innerHTML = results;
},
error: function(error){
alert("Error gathering customer information: " + error.code + " - " + error.message);
}
});
}
},
error: function(error){
alert("Error creating report :" + error.code + " - " + error.message);
}
});
}
Here is my cloud code for this function:
Parse.Cloud.define("runReport", function(request, response) {
var sDate = request.params.sDate;
var caseList = Parse.Object.extend("Cases");
var query = new Parse.Query(caseList);
query.equalTo("status", "closed");
query.greaterThanOrEqualTo("createdAt", new Date(sDate.toISOString()));
query.find({
success: function(results){
var q = results;
response.success(q);
},
error: function(error){
response.error("Failed to create query for report.");
}
})
});
Parse.Cloud.define("caseHelper", function(request, response) {
var id = request.params.id;
var className = request.params.className;
var attribute = request.params.attribute;
var list = Parse.Object.extend(String(className));
var query = new Parse.Query(String(className));
query.equalTo("objectId", id);
query.first().done(function(result){
var a = result.get(attribute);
response.success(a);
});
});