Skip to content

Unauthorized Save Object in Cloud Code #13

Description

@wagnercsfilho

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);
        }
    });
});

Activity

  1. gfosco commented on Jan 29, 2016

    @gfosco
    Contributor

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

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

  2. wagnercsfilho commented on Jan 29, 2016

    @wagnercsfilho
    Author

    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.

  3. gfosco commented on Jan 29, 2016

    @gfosco
    Contributor

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

    Parse.serverURL = 'http://localhost:1337/parse';
  4. wagnercsfilho commented on Jan 29, 2016

    @wagnercsfilho
    Author

    It's working! thank you.

  5. kingmatusevich commented on Jan 29, 2016

    @kingmatusevich

    @gfosco this can be closed now.

  6. jiawenzhang commented on Feb 11, 2016

    @jiawenzhang

    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

  7. drew-gross commented on Feb 11, 2016

    @drew-gross
    Contributor

    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.

  8. nick-merrill commented on Feb 11, 2016

    @nick-merrill

    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`
        }
    });
    
  9. nick-merrill commented on Feb 11, 2016

    @nick-merrill

    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

  10. jiawenzhang commented on Feb 12, 2016

    @jiawenzhang

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

  11. jaydeep82 commented on Mar 10, 2016

    @jaydeep82

    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 !!

  12. chadpav commented on Apr 21, 2016

    @chadpav

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions