parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Running a cloud function from Index.js

Open bcootner opened this issue 9 years ago • 4 comments

Issue Description

I can not run a cloud code function (defined in main.js) from an express route (index.js)

In index.js

app.post('/test',  function (req, res) {
  demoFunction(req.body.input).then(function(output){
    //do some function
  })
});

var demoFunction = function (passedInput) {
  console.log("demoFunction start")
  return Parse.Cloud.run("cloudCodeFunction", {input: passedInput}, {useMasterKey: true}).then(function (output) {
       //function
        return output.info;
      })
      .catch (function(err) {
        console.log("ERROR!")
        console.log(err);
      })   
}

In main.js

 Parse.Cloud.define("cloudCodeFunction", function(request, status){ 
        console.log('ranCloudFunc');
        var query = new Parse.Query("myClass");
        query.equalTo("field", request.params.input)
        query.find({
            useMasterKey: true,
            success: function(objects) {
              response.success(objects[0]);
            }, error: function(error) {
                response.error(error)
            }
        });
    })

Expected Results

Cloud code function to run when a POST request is made to /test Console logs should have: demoFunction start ranCloudFunc

Actual Outcome

Console logs have: demoFunction start ERROR! ParseError { code: undefined, message: 'unauthorized' }

and the cloud code function does not get performed.

Environment Setup

  • Server
    • parse-server version: 2.2.18
    • Remote server: Heroku

bcootner avatar Dec 17 '16 20:12 bcootner

I don't think your syntax is quite right on useMasterKey in main.js.

Try adjusting your useMasterKey syntax to this:

    var query = new Parse.Query("myClass");
    query.equalTo("field", request.params.input);
    query.find({ useMasterKey: true })
      .then(function(objects) {
        response.success(objects[0]);
    }, function (error)
      response.error(error)
    });

OpConTech avatar Dec 17 '16 20:12 OpConTech

@OpConTech Nope, that still doesn't work. I don't think theres a problem with the function in main.js since the console debug line 'ranCloudFunc' never gets printed to the console. This makes me think the error is happening at the Parse.Cloud.run("cloudCodeFunction line

bcootner avatar Dec 17 '16 20:12 bcootner

Have you tried running your cloudCodeFunction by itself without the calling it so that you are sure that it works? That error message you are getting where it says "unauthorized" will happen when useMasterKey: true syntax is wrong and makes it by the compiler but still does not work correctly.

OpConTech avatar Dec 18 '16 20:12 OpConTech

@bcootner I managed to reproduce what you were trying, and I succeeded running a cloud function without any problems from index.js.

In your main.js file:

Parse.Cloud.define("cloudCodeFunction", function(request, response) {
  response.success("Hello " + request.params.input);
});

In your index.js file:

app.get('/goodtest/:input',  function (req, res) {
  goodDemoFunction(req.params.input).then(function(output) {
    res.send(output);
  })
});

var goodDemoFunction = function (passedInput) {
  console.log("demoFunction start")
  return Parse.Cloud.run("cloudCodeFunction", {input: passedInput}, {useMasterKey: true});
}

Run npm start and then in another terminal tab run: curl http://localhost:1337/goodtest/world, you'll see it works.

If your demo function in index.js is returning a promise, you cannot use .then() in both places, inside the function and inside your app.post or app.get.

And besides that, on a side note, remember that the Parse JS SDK always returns a Parse.Promise, and they don't have a catch function. Take a look here.

natanrolnik avatar Jan 09 '17 10:01 natanrolnik