Skip to content Skip to sidebar Skip to footer

How To Chain Functions In Parse Cloudcode?

I've done a parse job that checks every 'X' time if 'emailSent' is false, for each user. If it is, I call a function to send a email and change the 'emailSent' to true. That works.

Solution 1:

There are a few things that need fixing in the code: (1) as a rule, use promises if you're doing more than two consecutive asynchronous things, (2) don't call Parse.Cloud.run from cloud code, it's what you call from clients who wish to invoke cloud functions, (3) style-wise, you'll go nuts trying to figure it out later on unless you break the code into small, promise-returning steps.

I've applied all three bits of advice to your code. I don't fully understand the logic as described in code and text, but hopefully I got close enough for you to make sense of it.

// using underscore js, which provides _.map below as well as loads of other useful stuffvar _ = require('underscore');

// return a promise to get the max value of id_client in the user tablefunctiongetMaxId() {
    var query = newParse.Query(Parse.User); 
    query.descending("id_client");
    query.limit(1);
    return query.find().then(function(users) {
        return (users.length)? users[0].get("id_client") : 0;
    });
}

// return a promise for users with emailSent flag == falsefunctionusersWithUnsentEmail() {
    var query = newParse.Query(Parse.User);
    query.equalTo("emailSent", false);
    return query.find();
}

// return a promise to send email to the given user, and to set its // emailSent flag = true, and to set its clientId to the passed valuefunctionsendEmailToUser(user, idClient) {
    returnsendEmail(user.get("email")).then(function() {
        user.set("emailSent", true);
        user.set("id_client", idClient);
        return user.save();
    });
}

// return a promise to send email to the given email address via an http servicefunctionsendEmail(email) {
    var params = {url: 'http://www.example.com/sendemail.php', params: {email : email} };
    returnParse.Cloud.httpRequest(params);
}


Parse.Cloud.job("test", function(request, response) {
    // Set up to modify user dataParse.Cloud.useMasterKey();

    var maxIdClient;
    getMaxId().then(function(result) {
        maxIdClient = result;
        returnusersWithUnsentEmail();
    }).then(function(users) {
        var emailPromises = _.map(users, function(user) {
            returnsendEmailToUser(user, maxIdClient);
        });
        returnParse.Promise.when(emailPromises);
    }).then(function(results) {
        response.success(results);
    }, function(error) {
        response.error(error);
    });
});

EDIT - we're kind of working on logic here particular to the app, as opposed to the concept of promises, but here goes anyway. To restate the functional requirement: We want a job to find users who have not yet been recorded in another database, represented by a flag called "emailSent". Our goal is to assign these users a unique id, and send their info (for now, we'll say email address and that id) via email to some fixed destination.

So

// getMaxId() from above is almost ready, except the minimum id_client// value is 20000, so change the line that set this to:return (users.length)? users[0].get("id_client") : 20000;

// usersWithUnsentEmail() from above is fine// delete sendEmailToUser() since we're not doing that// change sendEmail() to take an array of users to be conveyed to// the other database. Send email about them, then change each user's// emailSent status and save themfunctionsendEmail(users) {
    var params = {url: 'http://www.example.com/sendemail.php', params: {users : JSON.stringify(users)} };
    returnParse.Cloud.httpRequest(params).then(function() {
        _.each(users, function(user) {user.set("emailSent", true);});
        returnParse.Object.saveAll(users);
    });
}

// add a function that takes an array of users, updates their// id_client to be a new, unique value, and sends mail about them// to a remote serverfunctionsynchUsers(users, idMax) {
    _.each(users, function(user) {
        user.set("id_client", idMax);
        idMax += 1;
    });
    returnsendEmail(users);
}

// update the job taking all this into accountParse.Cloud.job("test", function(request, response) {
    // Set up to modify user dataParse.Cloud.useMasterKey();

    var maxIdClient;
    getMaxId().then(function(result) {
        maxIdClient = result;
        returnusersWithUnsentEmail();
    }).then(function(users) {
        returnsynchUsers(users, maxIdClient+1);
    }).then(function(results) {
        response.success(results);
    }, function(error) {
        response.error(error);
    });
});

Post a Comment for "How To Chain Functions In Parse Cloudcode?"