4

I'm looking a method to automatically updating data using cloud code.

Let say I have a class Table. inside of it, I have three column : firstname, lastname, and fullname.

Currently, I only have firstname and lastname data only. Column fullname is still empty.

Is it possible to fill the fullname automatically, by just combining the value in firstname and lastname?

Thank you,

Gibran
  • 934
  • 2
  • 8
  • 19

2 Answers2

2

@RoyH is 100% right to maintain your computed column as new objects are created. To do an initial migration, try a cloud function, like:

var _ = require("underscore");

Parse.Cloud.define("addFullnames", function(request, response) {
    // useMasterKey if the calling user doesn't have permissions read or write to Table
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query("Table");
    // we'll call tables > 1000  an 'advanced topic'
    query.limit = 1000;
    query.find().then(function(results) {
        _.each(results, function(result) {
            var firstname = result.get("firstname") || "";
            var lastname = result.get("lastname") || "";
            result.set("fullname", (firstname + " " + lastname).trim());
        });
        return Parse.Object.saveAll(results);
    }).then(function(results) {
        response.success(results);
    }, function(error) {
        response.error(error);
    });
});

Call it like this:

curl -X POST \
  -H "X-Parse-Application-Id: your_app_id_here" \
  -H "X-Parse-REST-API-Key: your_rest_key_here" \
  -H "Content-Type: application/json" \
  https://api.parse.com/1/functions/addFullnames

You can factor out the code inside _.each() to make a function that's called here and by a beforeSave hook to maintain the data as it is added.

danh
  • 62,181
  • 10
  • 95
  • 136
  • thank you for your help. I've tried your code, but i got this error message : `{"code":141,"error":"TypeError: Cannot call method 'each' of undefined\n at main.js:11:40\n at e (Parse.js:2:8151)\n at Parse.js:2:7600\n at Array.forEach (native)\n at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:661)\n at c.extend.resolve (Parse.js:2:7551)\n at null.\u003canonymous\u003e (Parse.js:2:8230)\n at e (Parse.js:2:8151)\n at Parse.js:2:8904\n at g (Parse.js:2:8641)"}` – Gibran Apr 01 '15 at 13:25
  • 1
    Pls see edit. Make sure to require underscorejs which is really useful. Or replace the each with a native for loop. – danh Apr 01 '15 at 13:31
1

Yes, you can either just put a value for "fullname" when you are saving "firstname" and "lastname" (before cloudcode). Alternately, you can use the before save/ after save cloudcode functions to insert a value into that column:

Take a look at:

https://parse.com/docs/cloud_code_guide#webhooks-beforeSave https://parse.com/docs/cloud_code_guide#webhooks-afterSave

royherma
  • 4,095
  • 1
  • 31
  • 42
  • thank you for your answer. I think I forgot to mention that, I already got my data. So, I don't want to do "save the data". From my understanding, "afterSave" or "beforeSafe" will be execute every time "save the data" was execute. Is there any other method for this? – Gibran Mar 30 '15 at 22:29