Mobile apps by TuneSoftware

Parse Cloud Code with Multiple Javascript Source Files

Parse stuff for non-javascript developers

I’m using Parse as the backend platform for my new app, for both Android and iOS. It’s very simple to integrate into apps using their native sdk; and very powerful, with lots of features like caching, offline use, push notifications, and of course an excellent NoSql database. Also, the documentation is pretty good and there is quite an active community around it, with loads of questions and answers from developers on their own forum, and on stackoverflow.

One thing I like about it is the fact that you don’t need to write any server code, but if you do want to put some of the business logic on the server you can write cloud code using javascript. By doing this you can centralise code that can be used by all of your client apps.

To get you started writing cloud code they provide you with an initial main.js file for you to edit with your own code, and an easy way to deploy this to your area of the platform. So I started putting some javascript functions in main.js and calling these from my Android and iOS apps – very easy and very effective, even though I’m not a javascript developer. I also added some before and after save functions that are called whenever data is inserted into tables – just like triggers in old-fashioned RDBMS databases.

Very soon main.js started to get quite big so I wanted to split it into multiple source files. But when I created a new .js file and deployed it, I got a function not found error for the function in that file. Surprisingly the Parse documentation doesn’t cover this very common scenario, so after a bit of digging around I learned the first multi-file lesson:

I. To use multiple files you must include them in your main.js file.

Even though I deployed the new file called profile.js, Parse doesn’t recognise it unless you reference it in your main.js file like this

1

require("cloud/profile.js");

Put this require statement at the top of main.js for each of your source files and your good to go with as many files as you like.

The next thing I wanted to do, but struggled with, was putting a common function in a source file so that it could be called from any other source file. I created a function for posting push notifications in a file called push.js:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

// send a simple push message to the specified user

functionsendPush(user,message){

varquery=newParse.Query(Parse.Installation);

query.equalTo("user",user);

Parse.Push.send({

where:query,

data:{

alert:message,

title:"My App"

}

},{

success:function(){

console.log("push sent to "+user);

},

error:function(error){

console.error("error sending push "+error.code+": "+error.message);

}

});

}

And then tried to call it from another source file by adding a require at the top of the file
require("cloud/push.js"); and calling the function from within another function with
sendPush(user,"push message");. Again this gave me an error about unknown function.

After a bit of Googling I found some posts on a similar subject for Node.Js, so I tried implementing it as they said, and it worked. So lesson number two:

II . To use common functions you need to export them

So it appears Parse is using Node.Js or something similar, even though I haven’t seen any reference to it in the documentation, and by following the syntax for that you can get a lot more done in Parse.

This is how you do it. First you need to export the function by changing its signature to this:

1

2

exports.sendPush=function(user,message){

}

Then change the require statement in the other file to this syntax:

1

varpush=require("cloud/push.js");

Then when you want to call the function from within that file, use this syntax:

1

push.sendPush(user,"message text");

So, very easy when you know how, its just that its not documented by Parse. I suppose they assume if you’re writing server javascript code you know about this stuff.

Anyhow, going to watch Location, Location, Location on catchup now, Phil and Kirstie are always good entertainment on a Saturday morning.