我理解分析承諾背後的理論(.then,.done,.when等),但我不知道如何執行它們。目前,我正在使用警報來迫使系統等待足夠長的時間以履行承諾,但那是一種非常粗糙的做法。我將發佈的代碼完全符合我的要求。它創建一個表格,從數據庫收集信息,並將其格式化到表格中。如果我拿出戒備(「迫使等待」);它不會工作,因爲承諾還沒有實現。我應該在哪裏以及如何添加一些承諾處理程序以使其在沒有警報的情況下工作?我嘗試將for循環更改爲do,然後添加。然後,但我無法讓它不拋出錯誤,但是我很確定這是因爲我格式化了錯誤的結構。任何幫助將不勝感激。謝謝!執行分析承諾
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);
}
});
}
這裏是我的這個功能雲代碼:
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);
});
});
你從哪裏運行'billingReport'?它與'runReport'在同一個文件中嗎?從其他地方調用runReport嗎?就像從Parse以外的應用或網站一樣? – MayNotBe