Executing Parse Promises

2019-09-04 04:12发布

问题:

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);
        });
});

回答1:

I might be wrong, but Parse.Cloud.run("caseHelper", might be causing the problem, all of these are run in async, without alert result of 2 might come ahead of result of 1 and screw up the table.insertRow(i), also not sure of the reason behind [i-1] business, because at least for i value 0, it wont work.

I would suggest you to use Promise.when and wait for all the promises to finish before inserting the rows:

function billingReport(){
    var sDate = new Date(document.getElementById("startDate").value);
    var table = document.getElementById("results1"); 
    var row, cell1, cell2, cell3, cell4, scores; 
    var tableHeaderRowCount = 1;
    var rowCount = table.rows.length;
    for (var i = tableHeaderRowCount; i < rowCount; i++) {
        table.deleteRow(tableHeaderRowCount);
    }
    Parse.Cloud.run("runReport", {sDate: sDate}).then(function(data){
        scores = data;
        console.log("Successfully retrieved " + scores.length + " scores.");
        return Parse.Promise.when(scores.map(function(score){
            return Parse.Cloud.run("caseHelper", {id: score.attributes.customer.id, className: "User", attribute: "username"});
        }));
    }).then(function(results){
        for(var i = 0; i < scores.length; i++){ 
            row = table.insertRow(i);     
            cell1 = row.insertCell(0);
            cell2 = row.insertCell(1);
            cell3 = row.insertCell(2);
            cell4 = row.insertCell(3);  
            cell1.innerHTML = scores[i].id;
            cell2.innerHTML = scores[i].attributes.title;
            cell3.innerHTML = scores[i].attributes.hoursWorked;
            cell4.innerHTML = results[i];                    
        }
    }).fail(function(err){
        console.log("Error creating report :" + err);
    });
}