0

我正在尝试在 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.
            }
        });
    });

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

4

1 回答 1

1

Parse.Cloud.run返回一个Promise更多关于 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.
});
于 2013-03-24T11:54:31.213 回答