Parse-server: Unauthorized Save Object in Cloud Code

Created on 29 Jan 2016  路  12Comments  路  Source: parse-community/parse-server

Hello everyone! I am unable to save an object in cloud code . I arrive in the method, but every time I save is return "code : 141 - unauthorized " . Are there any special permission to cloud code in parse server?

my code:

Parse.Cloud.define("invite", function(request, response) {
    var invite= new Parse.Object("Invite");
    invite.set("phone", request.params.phonenumber);
    invite.save(null, {
        success: function(newInvite) {           
                response.success("SMS sent!");
        },
        error: function(user, error) {
            response.error("Error: " + error.code + " " + error.message);
        }
    });
});

Most helpful comment

My issue was related to the 'userMasterKey' requirement. What I didn't understand was the .save() function needs this as a second argument.
userData.save(null, { useMasterKey: true });
not
userData.save({ useMasterKey: true });

Guess I need to read the method signatures for every call.

All 12 comments

Yes we should update the docs for this, when using Parse as a client, you need to update the server address..

Parse.serverURL = "https://.....";

Ok , I'm running the local server with javascript sdk client perfectly, just not working manipulate objects in local cloud code . I will wait for the update documentation.

In your case, at the top of the cloud code file, try:

Parse.serverURL = 'http://localhost:1337/parse';

It's working! thank you.

@gfosco this can be closed now.

Adding Parse.serverURL = 'https://fast-badlands-XXX.herokuapp.com/parse' at the top of the cloud/main.js does not work for me. Query in cloud code always return error: unauthorized

If I use Parse.Cloud.useMasterKey(); query worked

Instead of Parse.Cloud.useMasterKey() you need to pass useMasterKey: true to each query you are making. Setting the parse server url in your cloud code file is unnecessary.

I'm using useMasterKey: true as a query parameter in the following manner, and I'm still getting an unauthorized error message. Is there something I'm missing? This query works when I wrap it in a Parse.Cloud.define function.

The following is wrapped in a Parse.Cloud.beforeSave("StudentFieldStudent", function(request, response) {...}); call.

var StudentFieldStudent = Parse.Object.extend('StudentFieldStudent');
var query = new Parse.Query(StudentFieldStudent);
query.equalTo('student', request.object.get('student'));  // `student` here is a Parse Object passed from the object requested to be saved
query.first({
   useMasterKey: true,
   success: function(object) {
        if (object) {
            response.error("A StudentFieldStudent with this `student` already exists.");
        } else {
            response.success();
        }
    },
    error: function(error) {
        response.error("Could not validate uniqueness for this StudentFieldStudent object.");
        console.log(error.message);    // This prints `unauthorized`
    }
});

Apologies. Thought I had followed the above advice already with the serverURL; apparently I did not....

Setting serverURL with the new ParseServer({...}) function works perfectly.

Ref 1
Ref 2

I have to set serverURL AND useMasterKey to make query to work.

Hi All,

I'm also facing similar issue while trying to update the User Object.

In console log, I can only see "before save" message and then after couple of seconds it shows error.

Parse.Cloud.define('updateUserData',function(request,response)
{    
    //var userObjectId = request.params.userId;
    var userObjectId = "BPAJJCNdR1";

    if(userObjectId == null){
        response.error("UserId should not be null");
    }

    var User = Parse.Object.extend("_User");
    var userQuery = new Parse.Query(User);

    userQuery.equalTo('objectId',userObjectId);

    userQuery.first({
        useMasterKey: true, 

        success:function(userData){

            console.log("before save");

            userData.set("status",2);
            userData.set("verificationCode", randomNumber);

            userData.save(null, { useMasterKey: true });

            console.log("after save");

        },
        error: function(error){
            console.log(error);
            response.error(error);
        }
    });


});

In heroku log, it shows Request timeout error.

2016-03-10T14:15:32.212544+00:00 heroku[router]: at=info method=POST path="/parse/classes/_User" host=myapp-dev2.herokuapp.com request_id=508de99b-3ac7-49d7-a295-9f7f8155b473 fwd="54.81.XX.XX" dyno=web.1 connect=1ms service=126ms status=200 bytes=1652
2016-03-10T14:15:32.223044+00:00 app[web.1]: before save
2016-03-10T14:16:02.001309+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/functions/updateUserData" host=myapp-dev2.herokuapp.com request_id=5efb70e5-c942-4d65-a701-fb29f03763ef fwd="123.201.XX.XX" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0

I'm able to call all other cloud function.

Could you assist me here ?

Thank you in advance !!

My issue was related to the 'userMasterKey' requirement. What I didn't understand was the .save() function needs this as a second argument.
userData.save(null, { useMasterKey: true });
not
userData.save({ useMasterKey: true });

Guess I need to read the method signatures for every call.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

okaris picture okaris  路  4Comments

dcdspace picture dcdspace  路  3Comments

omyen picture omyen  路  3Comments

ugo-geronimo picture ugo-geronimo  路  3Comments

lorki picture lorki  路  3Comments