Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running a cloud function from Index.js #3253

Closed
bcootner opened this issue Dec 17, 2016 · 4 comments
Closed

Running a cloud function from Index.js #3253

bcootner opened this issue Dec 17, 2016 · 4 comments

Comments

@bcootner
Copy link

bcootner commented Dec 17, 2016

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
@OpConTech
Copy link

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)
    });

@bcootner
Copy link
Author

bcootner commented Dec 17, 2016

@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

@OpConTech
Copy link

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.

@natanrolnik
Copy link
Contributor

natanrolnik commented Jan 9, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants