【问题标题】:Parse Cloud Code Ending Prematurely?解析云代码过早结束?
【发布时间】:2014-11-10 03:32:18
【问题描述】:

我正在编写一个作业,我想在 Parse 的后台每小时运行一次。我的数据库有两个表。第一个包含Questions 的列表,而第二个列出所有用户\问题协议对 (QuestionAgreements)。最初我的计划只是让客户自己计算QuestionAgreements,但我发现这会导致很多请求确实可以取消,所以我希望这个后台作业运行计数,并且然后用它直接在Question 上更新一个字段。

这是我的尝试:

Parse.Cloud.job("updateQuestionAgreementCounts", function(request, status) {
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query("Question");
    query.each(function(question) {
        var agreementQuery = new Parse.Query("QuestionAgreement");
        agreementQuery.equalTo("question", question);
        agreementQuery.count({
            success: function(count) {
                question.set("agreementCount", count);
                question.save(null, null);
            }
        });
    }).then(function() {
        status.success("Finished updating Question Agreement Counts.");
    }, function(error) {
        status.error("Failed to update Question Agreement Counts.")
    });
});

问题是,这似乎只在少数Questions 上运行,然后停止,在 Parse Dashboard 的作业状态部分显示为“成功”。我怀疑问题是它过早地返回。以下是我的问题:

1 - 我怎样才能防止它过早返回? (假设这实际上是我的问题。)

2 - 调试云代码的最佳方法是什么?由于这不是客户端,我没有办法设置断点或任何东西,不是吗?

【问题讨论】:

    标签: javascript parse-platform parse-cloud-code


    【解决方案1】:

    status.successcount 的异步成功调用完成之前被调用。为了防止这种情况,您可以在此处使用 Promise。检查Parse.Query.each 的文档。

    遍历查询的每个结果,为每个结果调用回调。如果回调返回一个 Promise,则迭代将不会继续,直到该 Promise 已实现。

    因此,您可以链接 count 承诺:

    agreementQuery.count().then(function () {
        question.set("agreementCount", count);
        question.save(null, null);
    });
    

    您也可以使用parallel promises 来提高效率。

    云代码中没有断点,这使得 Parse 很难使用。唯一的方法是使用 console.log 记录您的变量

    【讨论】:

    • 太好了,谢谢!我会对此进行调查,然后在我发现今晚有效后接受您的回答。
    • 你的回答给了我一个有用的开始,所以我会 +1 它,但它缺少太多部分,我无法将其标记为已接受。
    • 很好,你发现你需要一个数组来保持并行承诺。如果没有并行承诺,您也可以在 query.each 中返回 agreementQuery.count().then(function () {}),因为文档说在该承诺兑现之前迭代不会继续
    【解决方案2】:

    按照 knshn 的建议,我能够利用 Promise 来实现它,以便我的代码在运行成功之前完成。

    Parse.Cloud.job("updateQuestionAgreementCounts", function(request, status) {
        Parse.Cloud.useMasterKey();
        var promises = [];  // Set up a list that will hold the promises being waited on.
        var query = new Parse.Query("Question");
        query.each(function(question) {
            var agreementQuery = new Parse.Query("QuestionAgreement");
            agreementQuery.equalTo("question", question);
            agreementQuery.equalTo("agreement", 1);
    
            // Make sure that the count finishes running first!
            promises.push(agreementQuery.count().then(function(count) {
                question.set("agreementCount", count);
    
                // Make sure that the object is actually saved first!
                promises.push(question.save(null, null));
            }));
        }).then(function() {
            // Before exiting, make sure all the promises have been fulfilled!
            Parse.Promise.when(promises).then(function() {
                status.success("Finished updating Question Agreement Counts.");
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多