Debugging your SashiDo app's Cloud Code

Georgi N. Georgiev

So, you have created your SashiDo app, set up some cloud code and are now wondering what would be the best way to test it. Of course, one of the obvious solutions would be to do a git push every time, then test it through the API Console, another REST client, or your app directly. However, you and I both know that there has to be a better way, right?

Run a local instance of Parse Server

Running a local instance of the Parse Server with your Cloud Code is the first step you should take as it will allow you to iterate on your app much faster and consequently allow us to debug it. Here's how:

1. Clone the source of your app

You can go to your GitHub profile and find the repo of your app. It will be something like pg-app-your-app-id, or you can just use the link from the SashiDo Dashboard.

get git url

After you have the repo URL just run the following command in a terminal to clone the source:

git clone

This will download your app's source to a folder named my-sahido-app, change that to match your actual app name.

2. Install the npm modules of your app

This will require you to have node.js and npm installed on your system. We recommend node.js v8.6.0 as this is the version SashiDo uses, but later versions should work too.

npm install

3. Open the directory in your favorite Editor/IDE

Preferably, it should support Node.js debugging. In this article, we will use Visual Studio Code but others, such as WebStorm work just as well.

3.1. Configure your local Parse Server

Open index.js, where you will see the Parse Server configuration. Keep in mind that this configuration will only affect your local Parse Server. It will look something like this:

var port = process.env.PORT || 1337;
var api = new ParseServer({
    databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
    appId: process.env.APP_ID || 'appId',
    masterKey: process.env.MASTER_KEY || 'masterKey',
    serverURL: process.env.SERVER_URL || 'http://localhost:' + port + '/1',
    cloud: process.env.CLOUD_CODE_MAIN || 'cloud/main.js',

    liveQuery: {
        classNames: []
    }
});

Here, you can change things like your Application Id and Master Key. You will be running this Parse instance only locally, but it's still a good practice to change your Master Key. It's also recommended to run a local instance of MongoDB, here's a good tutorial from the MongoDB docs - Manage MongoDB processes. In case you want to use the same data as your SashiDo app, you can simply import it in your local MongoDB. We have a great tutorial on that - Database import & export.

3.2. Run it

After you have set it all up, it's time to run the Parse Server.:

npm start

3.3. Check it

Check that everything is working as expected by running:

curl

By the way, this is how the code for connecting a JavaScript SDK instance to your local server looks like, in case you would want to test some queries or test your mobile app. Note that to connect to your local Parse Server, you don’t need a JavaScript key, because by default it’s not initialized with one. When connecting to SashiDo, you will need one as per the instructions in Dashboard > Your app > Getting Started.

Parse.initialize('appId');
Parse.masterKey = 'masterKey';
Parse.serverURL = 'http://localhost:1337/1/';

Debug your cloud code

Alright, so we have started our local instance of the Parse Server, but what about the debugging part?

You have two options. The first one is to utilize console.info, console.warn and console.error. This is perfectly fine for most cases, but sometimes you might need to debug a more complex scenario, where the console just doesn't cut it. In such scenarios, you can use Node.js' debugging capabilities. The debugger will allow you to inspect variables, step through code and evaluate expressions on the fly. You can watch the video Getting started with Node.js debugging in VS Code for more detailed information.

As mentioned above, we will use Visual Studio Code for this example, however, if VSCode is not your cup of tea, the process is mostly the same for other Editors/IDEs.

You should have already opened your project in your editor. Now, click the buttons as shown in the picture below:

1-vscode

This will open a JSON file containing the following code:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/index.js"
        }
    ]
}

You don't need to change anything here, VSCode generated by default, a debugger configuration that runs index.js, which is the entry point of our application. Now, we can create a simple cloud function just to test our debugger:

Parse.Cloud.define('test', (req, res) => {
    console.log(req.params);
    res.success();
});

Run the following curl command to invoke the function from our terminal:

curl --request POST \
  --url http://localhost:1337/1/functions/test \
  --header 'content-type: application/json' \
  --header 'x-parse-application-id: myAppId' \
  --data '{
        "myparam": "myvalue"
}'

After this, all that is left to do is place a breakpoint inside the function and run the debugger as shown on the gif below.

debug

Related resources

  1. Node.js' debugging capabilities
  2. Getting started with Node.js debugging in VS Code
  3. Debugging in 2017 with Node.js

The end

Hopefully, after this, you will be able to debug your applications a little more easily and develop cool stuff even faster than before!

Georgi N. Georgiev

Software Engineer @ SashiDo

Find answers to all your questions

Our Frequently Asked Questions section is here to help.







Latests news

How to make your Mobile Application GDPR Ready

At SashiDo we certainly care about our customers and we strive to be up-to-date on the horizon of the novelties coming up. We’re all working on GDPR these days and we want to help people understand what it is. What does GDPR mean? In February we announced that GDPR is coming and SashiDo.io is getting ready and now we will be more specific on how to make sure your mobile app is prepared for the upcoming changes in the European Union region.

Read More

SashiDo Acquires Octobas

SashiDo is pleased to announce its recent acquisition of all the assets of Octobas.com, a robust and scalable backend for mobile and web apps based on Parse Open Source. For the past 2 years our mission has been to become a leading Parse Server hosting provider in the mobile and web apps development world as well as to continue improving our service, launching new features and providing quality and security to our customers.

Read More