4

I have a node.js express app using the Parse Javascript SDK and Parse-Server.

I've set everything up according to the guide but the JS SDK is not working.

I'm getting {error:unauthorized}. I can access my server with REST API just fine but not from within the app using the JS SDK.

I read that you have to specify useMasterKey = true for all operations with Parse-Server so:

var User = Parse.Object.extend('_User');
var query = new Parse.Query(User);
query.useMasterKey = true;

query.find().then( function(results) {
    console.log(results);
},
function(error) {
    console.log(JSON.stringify(error));
});

Should return the same data that this curl does (right?):

curl -X GET \
-H "X-Parse-Application-Id: myAppId" \
-H "X-Parse-Master-Key: myMasterKey" \
http://localhost:1337/parse/classes/_User

Alas, this is not the case.

Same error message when I try to run the default cloud code function:

 Parse.Cloud.run('hello').then( function(response) {
    console.log(response);
 },
 function(error) {
    console.log(JSON.stringify(error));
 }); 

Any ideas that might help? Thanks.

2
  • I am trying to do the same thing. I get the {error:unauthorized} when trying to access the url the Parse Server is located "baseURL/parse". Also, when using the javascript SDK within the express app setting up the Parse Server, the Cloud Code functions always return an error. Has anybody been able to get Parse Server and the Parse JS SDK running from an Express app? Commented May 4, 2016 at 22:58
  • I just posted my solution. Thanks for posting yours. I left out the Parse.initialize lines altogether (because the server does that) and I don't have to use the masterkey in my queries or cloud code. Everything just works. Commented May 5, 2016 at 15:38

2 Answers 2

4

Got it!

What did it for me were these lines in my Express app

 var Parse = require('parse/node').Parse;
 Parse.initialize('app_id', 'js_key','master_key');
 Parse.serverURL = 'serverURL';
 Parse.Cloud.useMasterKey();

After these lines, any kind of call that requires Cloud Code usage or Parse usage were authorized

Sign up to request clarification or add additional context in comments.

Comments

1

My problem was inexperience:

I am using body-parser and had the app set up to use that module's raw, json, text, and urlencoded middleware functions.

I only needed the urlencoded option. I removed the others and the JS SDK started working fine. Somehow, the other functions were interfering with the SDK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.