简体   繁体   English

解析云代码:承诺中的逻辑分支

[英]Parse Cloud Code: Logic Branching in Promises

I'm trying to write a Parse.com Cloud Code function to accomplish the following workflow: 我试图编写一个Parse.com云代码功能来完成以下工作流程:

  1. User submits a value. 用户提交一个值。
  2. Cloud code function checks to see if that value matches any objects of type code . 云代码功能检查该值是否匹配任何类型为code对象。
  3. If not, the function returns a "not found" value. 如果没有,该函数将返回“未找到”值。
  4. If so, the object of type code is assumed to have a pointer to an object of type item . 如果是这样,则假定类型为code的对象具有一个指向类型为item的对象的指针。
  5. Then, code.item is checked to see whether it has a pointer to an object of type alert . 然后,检查code.item以查看其是否具有指向alert类型的对象的指针。
  6. If not, the function returns a "not found" value. 如果没有,该函数将返回“未找到”值。
  7. If code.item.alert does exist, then I want to fetch the full alert object, including pointers which may or may not exist, up to 2 layers deep. 如果code.item.alert 确实存在,那么我想获取完整的alert对象,包括可能存在或可能不存在的指针(最多2层)。

As I begin writing the code for this function, I can get it working to the point of checking to see whether the code exists and, if so, whether code.item.alert also exists. 当我开始为该函数编写代码时,我可以使其工作到检查code是否存在以及是否存在code.item.alert

This is where the problem arises. 这就是问题所在。 As it currently stands, in the working version of my function, the alert item that is returned is only the class type and objectId. 就目前情况而言,在我的函数的工作版本中,返回的alert项仅是类类型和objectId。 I understand why that is happening, and I am trying to write code to populate the object before returning it, but I am failing in that attempt. 我了解为什么会发生这种情况,并且我尝试编写代码以在返回对象之前填充该对象,但是我没有成功。

Here's the code that is working so far (but only returning the alert object's shell): 到目前为止,这是起作用的代码(但仅返回alert对象的外壳程序):

Parse.Cloud.define("alertLookup", function (request, response) {
    Parse.Cloud.useMasterKey(); 

    var codeQuery       = new Parse.Query("code");  
    codeQuery.equalTo("value", request.params.code);
    codeQuery.include("item");

    codeQuery.find().then(function (codes) {
        if (codes.length === 0) {
            response.success("no item");
        } else {
            var code    = codes[0];
            var item    = code.get("item");
            var alert   = item.get("alert");
            if (alert === null || alert === undefined) {
                response.success("no item");
            } else {
                response.success(alert);
            }
        }       
    }, function (error) {
        response.error(error);
    });
});

Here's what I have tried that is failing with an error code of 141: 这是我尝试失败的错误代码为141的内容:

Parse.Cloud.define("alertLookup", function (request, response) {
    Parse.Cloud.useMasterKey(); 

    var codeQuery       = new Parse.Query("code");  
    codeQuery.equalTo("value", request.params.code);
    codeQuery.include("item");

    codeQuery.find().then(function (codes) {
        if (codes.length === 0) {
            response.success("no item");
        } else {
            var code    = codes[0];
            var item    = code.get("item");
            var alert   = item.get("alert");
            if (alert === null || alert === undefined) {
                response.success("no item");
            } else {
                return alert.fetch();                   
            }
        }       
    }).then(function (a) {
        response.success(a);

    }, function (error) {
        response.error(error);
    });
});

Why won't the fetch() call work properly? 为什么fetch()调用不能正常工作? When I insert console.log() statements, although alert is non-null, return alert.fetch(); 当我插入console.log()语句时,尽管alert不为null,但return alert.fetch(); does not ever seem to be called. 似乎从未被调用过。 At least, the response.success(a); 至少, response.success(a); line is never called. 线永远不会被调用。 Why not? 为什么不?

Try this instead while chaining Promises: 在链接Promises时尝试以下方法:

codeQuery.find().then(function (codes) {
    if (codes.length != 0) {
        var code    = codes[0];
        var item    = code.get("item");
        var alert   = item.get("alert");
        if (alert != null && alert != undefined) {
            var alertObj = new Parse.Object("alert"); // alert class ???
            alertObj.id = alert.id;
            return alertObj.fetch();                   
        }
    }
    // return a Promise for no items
    return Parse.Promise.as("no item");

}).then(function (a) {
    response.success(a);

}, function (error) {
    response.error(error);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM