3

So here's my problem, we're developing a mobile app so 5 month ago when we began, we used Parse for our backend solution. Now Parse will die in a few month but luckily, we can still use Parse server by including their API. But after migrating our DB (implying in the process a lot of anger and despair :p) on mongo and hosting the server on AWS, I cannot execute my cloud code anymore. The functions I defined are not found, and with the Parse dashboard not available anymore, I can't check if my cloud funcs are deployed on the server or not. Can someone help me and tell me what I am doing wrong ? And do you guys know another way to check on cloud funcs ? Here's my index.js :

var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var math = require('mathjs');

var databaseUri = process.env.DATABASE_URI || process.env.MONGOLAB_URI
var app = express();
Parse.initialize('XXXXXXX', 'XXXXXX');

if (!databaseUri) {
  console.log('DATABASE_URI not specified, falling back to localhost.');
}

var api = new ParseServer({
  databaseURI: databaseUri || 'XXXXXX',
  cloud: './cloud/main.js',
  appId: process.env.APP_ID || 'XXXXXX',
  masterKey: process.env.MASTER_KEY || 'XXXXXX',
  serverURL: 'http://parseserver-4w2hk-env.elasticbeanstalk.com/'
});


var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

app.get('/', function(request, response) {

  Parse.Cloud.run('hello');

});

and my main.js (cloud funcs) :

Parse.Cloud.define('hello', function(req, res) {
  Parse.Cloud.httpRequest({
      url: 'http://www.parse.com/',
      success: function(httpResponse) {
        response(httpResponse.text);
      },
      error: function(httpResponse) {
        response('Request failed with response code ' + httpResponse.status)
      }
    });
});

Here's the log line, it display a 404 error for the cloud function :

[21/Feb/2016:16:58:56 +0000] "POST /functions/hello HTTP/1.1" 404 29 "-" "node-XMLHttpRequest, Parse/js1.7.1 (NodeJS 4.2.3)"

Thanks in advance ! :)

5 Answers5

4

You should be able to call the function with an HTTP request.

Did you try the sample cloud code:

Parse.Cloud.define('hello', function(req, res) {
  res.success('Hello world!');
});

Note that the default mount path is '/parse' and not '/1' as it was with the parse.com service.

For a test using curl:

curl -X POST -H "X-Parse-Application-Id: XXX" -H "X-Parse-REST-API-Key: XXX" http://<your server>/parse/functions/hello
Ilya
  • 5,377
  • 2
  • 18
  • 33
  • I just tried the make a simple request to the parse website. I tried the sample code too, it didnt change anything. The function just doesn't execute at all, whatever the contents are. Yup I did that : var mountPath = process.env.PARSE_MOUNT || '/parse'; app.use(mountPath, api); – Alexandre DI MAMBRO Feb 21 '16 at 17:51
  • You can first do a console.log to check that it's called, the log should be visible on AWS (?) – Ilya Feb 21 '16 at 17:53
  • I did, If i do a console.log in the cloud function, it wont be displayed (and I surrounded the function call with two console.log that appeared) – Alexandre DI MAMBRO Feb 21 '16 at 17:55
  • Is it working if you call it directly with a HTTP request ? – Ilya Feb 21 '16 at 17:57
  • What URL should I use to do that ? I managed to do it when I had a parse server but I dont see at all how to do it now – Alexandre DI MAMBRO Feb 21 '16 at 18:02
  • Sorry i'm gonna need a little more help If you have the time, I'm using postman in order to do that but when I try to access it gives me error 403 forbidden. Do you know what I should put in the parameter in order for it to work ? Or do you have another solution i can use ? thanks :) – Alexandre DI MAMBRO Feb 21 '16 at 18:08
  • Can't you just access the url in a browser ? – Ilya Feb 21 '16 at 18:10
  • No, I'm also getting 403 error (which I think is kinda logical, we do not want everyone accessing our cloud funcs, but only the apps authorized by parse) Edit : so I tried to put that in my index.js but it gives me a 502 BAd gateway Parse.Cloud.httpRequest({ url: 'http://parseserver-4w2hk-env.elasticbeanstalk.com/parse/functions/hello', method: 'POST' success: function(httpResponse) { response(httpResponse.text); }, error: function(httpResponse) { response('Request failed with response code ' + httpResponse.status) } }); – Alexandre DI MAMBRO Feb 21 '16 at 18:13
  • So it's an authorization problem and not 404 any more ? Could you relax the authorization for that test ? – Ilya Feb 21 '16 at 18:20
  • About your last test: I'd stick with the simple test function (hello), because that code is guaranteed to work. – Ilya Feb 21 '16 at 18:29
  • @AlexandreDIMAMBRO I've added a test with curl in the answer, on postman it should be similar (I don't use postman so can't help with that). – Ilya Feb 21 '16 at 19:42
  • I don't think I am authorized to just access to this part of the server. For the function, yeah I can leave the sample as it is guaranteed to work but as long as I have 404 error when i try to access my functions, it won't resolve my problem (sorry for the late answer) – Alexandre DI MAMBRO Feb 21 '16 at 20:21
  • Is it 404 or 403 ? Did you try in Postman or with curl with the right (Id/key) headers ? If you don't have the authorization, how the cloud functions are supposed to work or be tested ? – Ilya Feb 21 '16 at 20:28
2

Use cloud: __dirname+'/cloud/main.js', instead of cloud: './cloud/main.js',

Rohit
  • 1,001
  • 1
  • 11
  • 20
0

So I have some more infos : when I do a curl POST command on my serverurl/parse/functions/hello, it works, but it gives me "Hello World" as response, so that means that my file main.js has never been uploaded on the cloud code. I am sure it has something to do with my ParseServer configuration, but I dont know what....Maybe it's because of the replicaSet, there might be another way to do it.

0

Yeah so, I don't know why, when I changed our dev environment and went on the prod, cloud functions were magically executed, so I cloned that env, and now it's working, my cloud funcs are called properly. Ahhhh the wonders of code.. Thanks anyway guys and thank you Ilya for taking some time :)

0

For hosted parse server you must restart your server every time you add a new cloud function to /cloud/main.js

regards