Where should I place cloud code in parse (with EC2 AWS)

Hi!

I have a successful installation of Parse server and parse dashboard running on an AWS EC2 machine (using bitnami parse server in the market place at amazon console version 3.9.0.1). I can query the database from my app with no problem (all operations including getting, deleting, saving and updating objects). Recently I decided to include cloud code.

This is what I did:

  • In the route /opt/bitnami/apps/parse/htdocs I created a directory named cloud
  • In this directory cloud I created a file main.js with my cloud function, the file content is:
Parse.Cloud.define('hello', function(req, res) {
            return 'Hi'; 
        });
  • In the route /opt/bitnami/apps/parse/htdocs edit the file server.js. I changed, in
var api = new ParseServer(
{databaseURI: ...,
cloud: "./node_modules/parse-server/lib/cloud-code/Parse.Cloud.js",
appId: ...,
javascriptKey: ...,
masterKey: ...,
fileKey:...,
serverURL: ...});

the cloud attribute to ./cloud/main.js

  • I did the same for server.js.save
  • I ran sudo /opt/bitnami/ctlscript.sh restart

The server restarts successfully but when I try to run my cloud function on my app the response is ParseError: 141 Invalid function: "hello"

What am I doing wrong? I see the logs and it seems that ./cloud/main.js is recognized as a valid directory (I know this beacause I made a mistake once an introduced the name of the directory wrong and the log said Error: couldn’t find module)

In the following post the author says he followed the exact same procedure to include cloud code, so I’m a little bit confused (I also tried deleting the directory cloud and creating it again after changing the files)

In the logs I have

error: Parse error: Invalid function: "hello" {"code":141,"stack":"Error: Invalid function: \"hello\"\n    at handleCloudFunction (/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/lib/Routers/FunctionsRouter.js:134:13)\n    at /opt/bitnami/apps/parse/htdocs/node_modules/parse-server/lib/PromiseRouter.js:175:7\n    at Layer.handle [as handle_request] (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/layer.js:95:5)\n    at next (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/route.js:137:13)\n    at Route.dispatch (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/route.js:112:3)\n    at Layer.handle [as handle_request] (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/layer.js:95:5)\n    at /opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:281:22\n    at param (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:354:14)\n    at param (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:365:14)\n    at Function.process_params (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:410:3)"}
error: Error handling request: { Error: Invalid function: "hello"
    at handleCloudFunction (/opt/bitnami/apps/parse/htdocs/node_modules/parse-server/lib/Routers/FunctionsRouter.js:134:13)
    at /opt/bitnami/apps/parse/htdocs/node_modules/parse-server/lib/PromiseRouter.js:175:7
    at Layer.handle [as handle_request] (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/layer.js:95:5)
    at next (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/layer.js:95:5)
    at /opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:281:22
    at param (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:354:14)
    at param (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/opt/bitnami/apps/parse/htdocs/node_modules/express/lib/router/index.js:410:3) message: 'Invalid function: "hello"', code: 141 } {"error":{"message":"Invalid function: \"hello\"","code":141}}

Hi Jd, welcome to the community :wave:

Your cloud function is a mix of 2.x syntax and 3.x syntax. Can you tell me either the version of Parse Server are you running or the version of the Bitnami AWS image?

If your Parse Server version is 3.0.0 or above your cloud function should look like this:

Parse.Cloud.define('hello', (request) => {
  return 'Hi';
});

Or for 2.x it should look like this:

Parse.Cloud.define('hello', function(request, response) {
  response.success('Hi');
});
1 Like

Hi Tom,

I’m using version 3.10.0 (at least that’s what my Parse Dashboard says). I corrected my cloud function, now it is

Parse.Cloud.define('hello', (request) => {
  return 'Hi';
});

and ran sudo /opt/bitnami/ctlscript.sh restart and it still not working (same error as before invalid function). Thanks for your help :slight_smile:

@Tom Hello! I’ve been trying to implement cloud functions in my parse project but am currently having similar issues as @jd-chaves, the functions all appear invalid. Any help would be great! Keep the thread rollin’

1 Like

You need to restart the parse-server module to make cloud functions work properly.

They also need to be configured inside your parse-server index.js file. You need to include a URL to your cloud code file in the app declaration.

1 Like

Hi! Thanks for your answer. I’ve been doing what you suggest from the very beggining, that was not the problem. What did work for me was renaming the directory ‘cloud’ to ‘src’, no idea why but it worked

1 Like

Happy that you worked it out nonetheless! Sounds like maybe a config issue in your index.js but that’s just me guessing :slight_smile: