Skip to content

useMasterKey in cloudCode not working as expected #324

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

Closed
ktmatzdorf opened this issue Feb 9, 2016 · 6 comments
Closed

useMasterKey in cloudCode not working as expected #324

ktmatzdorf opened this issue Feb 9, 2016 · 6 comments
Labels
type:question Support or code-level question

Comments

@ktmatzdorf
Copy link

still getting an error with useMasterKey in save method (see #37 )
Error:

Uncaught internal server error. [TypeError: Cannot read property 'isMaster' of undefined] TypeError: Cannot read property 'isMaster' of undefined
at RestWrite.runBeforeTrigger.RestWrite.handleAnonymousAuthData.RestWrite.handleFacebookAuthData.RestWrite.transformUser.promise.then.RestWrite.handleSession.RestWrite.handleInstallation.RestWrite.runDatabaseOperation (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/RestWrite.js:650:17)
at /home/kai/finc3/parse/parse-server-example/node_modules/parse-server/RestWrite.js:82:17

  Parse.Cloud.define('test2', function(request, response) {
    SyncReport = Parse.Object.extend('SyncReport');
    report = new SyncReport;
    report.set('testcolumn', "testentry");
    console.error(report);
    report.save(null, { useMasterKey: true })
    .then(function(user) {
      return responses.success(user);
    }, function(error) {
      console.error(error);
      return response.error(error);
    });
  });

parse-server is 2.0.7. MasterKey is set in ParseServer

@gfosco
Copy link
Contributor

gfosco commented Feb 11, 2016

And you're not requiring/defining Parse in your cloud code file, right?

@ktmatzdorf
Copy link
Author

Yes, i don´t require Parse in my cloud code. Not using a valid session as well. Calling the function via curl with app-id set.
With 2.0.8 i am getting

Uncaught internal server error. [TypeError: Cannot set property auth of # which has only a getter] TypeError: Cannot set property auth of # which has only a getter
at handleParseHeaders (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/lib/middlewares.js:103:14)
at Layer.handle as handle_request
at trim_prefix (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:312:13)
at /home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:330:12)
at next (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:271:10)
at allowMethodOverride (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/lib/middlewares.js:139:3)
at Layer.handle as handle_request
at trim_prefix (/home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:312:13)
at /home/kai/finc3/parse/parse-server-example/node_modules/parse-server/node_modules/express/lib/router/index.js:280:7

@ktmatzdorf
Copy link
Author

Another example for just making an query instead of creating an object on my local parse-server with a remote mongolab db (migrated via parse ui dashboard):

Parse.Cloud.define('getAllSyncErrors', function(request, response) {
  var query = new Parse.Query('SyncError');
  query.find({useMasterKey:true}).then(function(objAgain) {
    response.success(objAgain.toJSON());
  }, function(err) {response.error(err); });
  });

curl -X POST -H "X-Parse-Application-Id: Y6r8UU......" -H "Content-Type: application/json" -d '' http://localhost:1337/parse/functions/getAllSyncErrors

{"code":141,"error":{"code":107,"message":"Received an error with invalid JSON from Parse: <html>\r\n<head><title>405 Not Allowed</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>405 Not Allowed</h1></center>\r\n<hr><center>nginx/1.1.19</center>\r\n</body>\r\n</html>\r\n"}}

With parse-server 2.0.8 and index.js like this

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

var api = new ParseServer({
  databaseURI: 'mongodb://test:Gf....t@ds0....mongolab.com:59155/f....test',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: 'Y6r8UU....',
  masterKey: 'WBj4E...',
  serverURL: 'http://localhost:' + port + '/parse'
});
// Client-keys like the javascript key or the .NET key are not necessary with parse-server
// If you wish you require them, you can set them as options in the initialization above:
// javascriptKey, restAPIKey, dotNetKey, clientKey

var app = express();

// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
  res.status(200).send('I dream of being a web site.');
});

var port = process.env.PORT || 1337;
app.listen(port, function() {
    console.log('parse-server-example running on port ' + port + '.');
});

@mchun
Copy link

mchun commented Feb 19, 2016

@ktmatzdorf did you try report.save(null, { "useMasterKey": true }) with double quote instead?

I am able to use master key in the cloudcode as following:

    var noti = new Parse.Object("Notification");
    noti.id = notiIdList[i];
    noti.set("read",true);
    noti.save(null,{"useMasterKey":true});

@gfosco gfosco closed this as completed Feb 20, 2016
@gfosco
Copy link
Contributor

gfosco commented Feb 20, 2016

@mchun's example is correct. The session token or useMasterKey:true option must be passed to save/query calls, and the concept of the 'current' user does not function in node.

@daisyramos317
Copy link

Thanks @mchun this was helpful in my case!

@mtrezza mtrezza added type:question Support or code-level question and removed 🔧 troubleshooting labels Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Support or code-level question
Projects
None yet
Development

No branches or pull requests

5 participants