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

Running a cloud function from express route

Open andyburgess1978 opened this issue 10 years ago • 5 comments

Using the new parse server is there a way to call a function in the main.js file from the index.js? Example is to allow an endpoint for a third party so they can post a set of data without needed the application_id or any keys.

Old hosted parse.com allowed like this;

app.post('/test', function(req, res) {
    Parse.Cloud.run('functionname' data);
}

But I cannot get the routing to work in parse-server.

andyburgess1978 avatar Mar 14 '16 10:03 andyburgess1978

What code are you actually attempting to run?... Yes, it should work just fine, like any Javascript app using Parse. Your example lacks success/error handling, and data is undefined. Pretty sure you'll figure it out. If you find a bug, please open a new issue. Thanks.

gfosco avatar Mar 17 '16 10:03 gfosco

In index.js (of parse-server), can you just run a generic cloud function? Do you have to important anything else to do this? Thanks so much!

greggmojica avatar Apr 18 '16 18:04 greggmojica

I too am seeing some weird issues related to this topic. The issue occurs when calling cloud code from the express app functions, even if I pass the correct session token and data. Below is the function;

app.post('/user-is-admin', function(req, res) {

    var sessionToken = req.user.getSessionToken();
    Parse.Cloud.run('userIsAdmin', { userId: req.body.userId }, {
        sessionToken: sessionToken,
        useMasterKey: false
    }).then(function(result) {
        res.json({ isAdmin: result });
    },  function(error){
        res.json({ isAdmin: false });
    });
});

The cloud code function then has a undefined user on req.user;

Parse.Cloud.define('userIsAdmin',function (request,response) {
    //request.user = undefined;
    //request.master = true;
    return response.success(); //Whatever
};

It appears that the Parse.Cloud.run options are ignored for both sessionToken and useMasterKey.

I assume I am missing something critical, and would appreciate some assistance on this.

Roost avatar Sep 20 '16 14:09 Roost

Has anyone made an update/fix/work around on this issue?

bcootner avatar Dec 18 '16 05:12 bcootner

@bcootner This is what I do.

// Helper function to easily handle the connection between express router and the parse app.
// We need to pass the session token in the request, which doesn't seem to work with Parse.Cloud.run()
// Replaces Parse.Cloud.run() from within express router app.
`exports.runParseCloudFunction = function(urlPath, token, params) {

    if (!params) {
        params = {};
    }

    var buildVariables = config.buildVariables(buildEnvironment);
    var publicServerURL = buildVariables.publicServerURL;

    urlPath = urlPath.indexOf('/') == 0 ? urlPath.substring(1) : urlPath;
    var url = publicServerURL + "/functions/" + urlPath;

    var headers = {
        'X-Parse-Application-Id': buildVariables.appId,
        'X-Parse-REST-API-Key': buildVariables.masterKey,
        'X-Parse-Client-Key': buildVariables.clientKey,
        'X-Parse-Javascript-Key':buildVariables.javascriptKey,
        'Content-Type': 'application/json;charset=utf-8'
    };

    if (token) {
        headers['X-Parse-Session-Token'] = token;
    }

    return Parse.Cloud.httpRequest({
        url: url,
        method: "POST",
        headers: headers,
        body: params
    });
};

and then call exports.runParseCloudFunction("someCloudFunction", sessionToken, params)

Roost avatar Dec 18 '16 19:12 Roost