解析 CloudCode 执行顺序 [英] Parse CloudCode order of execution

查看:54
本文介绍了解析 CloudCode 执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次保存新笔记时,我都会尝试向具有读取权限的所有人发送推送消息.

Im trying to send a push message to everyone with read access every time a new note is saved.

在伪代码中,它应该获得 ACL.评估 ACL 中的每个成员并返回所有具有读取访问权限的用户的数组.然后向每个成员发送推送通知.

In pseudocode it should get the ACL. Evaluate each member in the ACL and return an array of all users with read access. Then send a push notification to each member.

我已经尝试过一项一项地运行单独的任务,并且它运行正常.但是,当我将所有内容放在以下代码中时,我得到了奇怪的结果.查看日志我可以看到它没有按我预期的顺序执行.我首先虽然 getACL 调用是一个异步调用,所以我试图实现承诺但没有运气.然后在获得 stackoverflow 的帮助后,我发现 getACL 不是异步的,因此代码应该可以正常工作,对吗?

I've tried running separate task one by one and it works properly. However when I put everything together in the following code I get strange results. Looking at the log I can see it not executing in order as I expect. I first though the getACL call was an asynchronous call so I tried to implement promises with no luck. Then after help from stackoverflow I find out that getACL is not asynchronous therefore the code should just work, right?

这就是我正在尝试的:

Parse.Cloud.afterSave("Notes", function(request) {
    var idsToSend = [];
    var i = 0;
    console.log("1 start");

    var objACL = request.object.getACL();
    var ACLinJSON = objACL.toJSON();
    console.log("2 ACL = " + ACLinJSON);

    for (var key in ACLinJSON) {
        if (ACLinJSON[key].read == "true") {
            idsToSend[i] = key.id;
            console.log("3 i = " + i + " = " + idsToSend[i]);
            i++;
        }
    }


    console.log("4 idsToSend = " + idsToSend);

    //lookup installations
    var query = new Parse.Query(Parse.Installation);
    query.containedIn('user', idsToSend);
    Parse.Push.send({
        where: query,
        data: {
            alert: "note updated"
        }
    }, {
        success: function() {
            console.log("Success sent push");
        },
        error: function(error) {
            console.error("can’t find user"); //error
        }
    });
});

这是我从解析日志中看到的响应

And this is the response I see from parse log

I2014-08-04T08:08:06.708Z]4 idsToSend =
I2014-08-04T08:08:06.712Z]2 ACL = [object Object]
I2014-08-04T08:08:06.714Z]1 start
I2014-08-04T08:08:06.764Z]Success sent push

一切都完全乱了??

如何按照上面写的方式执行上面的函数?

How can I execute the above function in the way it's written?

推荐答案

我发现日志在我运行时也没有按顺序排列,可能是时间问题或其他问题,当它们在运行时忽略顺序同一秒,我还做了其他测试,以确认事情确实在我自己的 Cloud Code 上按顺序运行......让我完全困惑了一段时间.

I've found the logs are not in order when I run things too, could be a timing issue or something, ignore the order when they're in the same second, I have done other tests to confirm things really do run in order on my own Cloud Code... had me completely confused for a while there.

您遇到的问题是日志 #3 永远不会被命中...尝试自行跟踪 ACLinJSON 以查看实际结构.当你将它附加到一个字符串时,它会输出 [object Object] 正如你所看到的,console.log(ACLinJSON); 代替.

The issue you're having is that log #3 is never being hit... try tracing ACLinJSON on it's own to see the actual structure. When you append it to a string it outputs [object Object] as you have seen, so do console.log(ACLinJSON); instead.

这是我见过的结构:

{
  "*":{"read":true},
  "Administrator":{"write":true}
}

基于此,我希望您的循环能够正常工作,但它可能具有不同级别的包装.

Based on that I would expect your loop to work, but it may have a different level of wrapping.

更新:

原来问题是在寻找字符串true"而不是布尔值true,因此解决方法是替换以下行:

Turns out the issue was looking for the string "true" instead of a boolean true, thus the fix is to replace the following line:

// replace this: if (ACLinJSON[key].read == "true") {
if (ACLinJSON[key].read == true) {

这篇关于解析 CloudCode 执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆