Running a cloud function from express route
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.
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.
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!
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.
Has anyone made an update/fix/work around on this issue?
@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)