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,

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)"}
Pls see edit. Make sure to require underscorejs which is really useful. Or replace the each with a native for loop.
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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.