0

I am trying to write cloud code on parse.com for the following. I have three classes User, Brand and Campaign. I keep LastLoginTime in the User class. Brand class has a relation named Campaigns to Campaign class. I need to retrieve LastLoginTime from user then get all the campaigns that have updatedAt date greater than LastLoginTime and finally I need to retrieve Brands related to these campaigns. Thus far I have been only able to make a function to get LastLoginTime. I have tried to use matchesKeyInQuery or chained functions but havent been able to solve. here is the code thus far.

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.
         }
     });
 });

The Above works, the below does not:

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.
            }
        });
    });

Is there a way or do I need to change my model.

imran
  • 13
  • 5

1 Answers1

1

Parse.Cloud.run returns a Promise (More on Promises for Parse here), alternatively you can send in callback as the third parameter.

Using callbacks your code should look somewhat like this:

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) {}
});

See the nasty nesting here? Using promises, your code will look something like this instead:

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.
});
hank
  • 3,748
  • 1
  • 24
  • 37
  • Above code with promises gives error right at the begining, cant figure out whats the problem: {"code":141,"error":"TypeError: Cannot set property 'success' of undefined\n at Object.t.Cloud.run (Parse.js:1:71298)\n at main.js:291:17"} – imran Mar 25 '13 at 19:50