【问题标题】:parse.com cloud nested functionsparse.com 云嵌套函数
【发布时间】:2013-03-24 01:30:38
【问题描述】:

我正在尝试在 parse.com 上为以下内容编写云代码。我有三个类用户,品牌和活动。我将 LastLoginTime 保留在 User 类中。 Brand 类有一个名为 Campaigns 到 Campaign 类的关系。我需要从用户那里检索 LastLoginTime,然后获取所有更新日期大于 LastLoginTime 的活动,最后我需要检索与这些活动相关的品牌。到目前为止,我只能创建一个函数来获取 LastLoginTime。我曾尝试使用 matchesKeyInQuery 或链式函数,但未能解决。这是到目前为止的代码。

Parse.Cloud.define("getLoginTime",function(request, response){
     var User = Parse.Object.extend("User");
     var query = new Parse.Query(User);
     var lastlogin;
     query.equalTo("objectId", request.params.objectId);
     query.first({
         success: function(object) {
             // The object was retrieved successfully.
             lastlogin = object.get("LastLoginTime");
             response.success(lastlogin);     // works well if left alone :)
             console.log("entered time");
             console.log(lastlogin);
         },
         error: function(object, error) {
             // The object was not retrieved successfully.
             // error is a Parse.Error with an error code and description.
         }
     });
 });

以上有效,以下无效:

Parse.Cloud.define("getNewCampaigns", function (request, response){
        var Campaign = Parse.Object.extend("Campaigns");
        var cquery = new Parse.Query(Campaign);
        var lastlogin;
        lastlogin = parse.Cloud.run("getloginTime",request.params.objectId);
        cquery.greaterThan("updatedAt",lastlogin);
        cquery.find({
            success: function(results) {
                // results is an array of Parse.Object.
                response.success(results);
            },

            error: function(error) {
                // error is an instance of Parse.Error.
            }
        });
    });

有没有办法或者我需要改变我的模型。

【问题讨论】:

    标签: cloud parse-platform relational chained


    【解决方案1】:

    Parse.Cloud.run 返回一个Promise (More on Promises for Parse here),或者您可以将回调作为第三个参数发送。

    使用回调,您的代码应该看起来像这样:

    Parse.Cloud.run("getloginTime", request.params.objectId, {
        success: function(lastlogin) {
            cquery.greaterThan("updatedAt",lastlogin);
            cquery.find({
                success: function(results) {
                    // results is an array of Parse.Object.
                    response.success(results);
                },
                error: function(error) {
                    // error is an instance of Parse.Error.
                }
            });
        },
        error: function(error) {}
    });
    

    看到这里讨厌的嵌套了吗? 使用 Promise,您的代码将看起来像这样:

    Parse.Cloud.run("getloginTime", request.params.objectId).then(function(lastlogin) {
        var cquery = new Parse.Query(Campaign);
        cquery.greaterThan("updatedAt", lastlogin);
    
        return cquery.find();
    }).then(function(results) {
        // results is an array of Parse.Object.
        response.success(results);
    }, function(error) {
        // error is an instance of Parse.Error.
    });
    

    【讨论】:

    • 上面带有承诺的代码一开始就给出错误,无法弄清楚问题所在:{"code":141,"error":"TypeError: Cannot set property 'success' of undefined\n在 Object.t.Cloud.run (Parse.js:1:71298)\n 在 main.js:291:17"}
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多