Create Cloud Code at Parse.com


Parse.com is a Baas service. Baas enables us to create smartphone application without building complicated server. And Parse.com can be used for free if our request and auto jobs are not so many.

I created one Cloud Code there. Here’s what I did.

環境

  • Ubuntu 15.04

What I Wanted to Do

Set another column value on receiving request from iPhone application.

Procedure

Parse.com Cloud Code can be deployed to 2 environment. Whichever will do.

  • Parse.com
  • Heroku

I chose Parse.com.

Setup

First, I needed to setup developing environment. I installed parse command, according to Parse.com document.

The way how to use parse command is on Parse.com document, or you can check it by executing parse --help.

Next, prepare the working directory by parse new. And account information, application for deployment, deployment destination (Heroku or Parse) and working directory in your local machine were required. At last, you should choose initial directory structure, blank or skeleton. I chose latter.

Then, the working directory was created at your current directory. It is similar to rails new command.

Development

In my case, before save trigger was required and I used beforeSave. There are other methods for creating trigger, beforeSave, afterSave, beforeDelete, afterDelete.

The cloud code should be written in JavaScript. And the first argument for beforeSave is the class name, which is we want to modify.

Function defined by beforeSave is, of course, executed before saving. It is executed when we change record on web browser and when we insert or update from other application.

The code should be written in main.js.

Explanation for the Function

Change data on saving Example data.

If the value of value field is in predefined list, it changes the values of a and b fields.

For preventing stop for exception, I added try and catch.

console is a utility for writing log. console.log, console.error, console.warn methods are available. The log they output can be seen on log page in Parse.com. And, console.error and console.warn write error log, too. The log can be also seen be parse logs command. Apart from that, trigger execution log is written automatically.

Deploy

Deploy with parse deploy command.

After deployment, the trigger automatically start working.

Log

Here’s log and error log example.

At Parse.com, we can setup job like cron, after defining the method as I did like above.