Skip to content
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

Unauthorized Save Object in Cloud Code #13

Closed
wagnercsfilho opened this issue Jan 29, 2016 · 12 comments
Closed

Unauthorized Save Object in Cloud Code #13

wagnercsfilho opened this issue Jan 29, 2016 · 12 comments

Comments

@wagnercsfilho
Copy link

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);
        }
    });
});
@gfosco
Copy link
Contributor

gfosco commented Jan 29, 2016

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

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

@wagnercsfilho
Copy link
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.

@gfosco
Copy link
Contributor

gfosco commented Jan 29, 2016

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

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

@wagnercsfilho
Copy link
Author

It's working! thank you.

@kingmatusevich
Copy link

@gfosco this can be closed now.

@jiawenzhang
Copy link

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

@drew-gross
Copy link
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.

@nick-merrill
Copy link

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

@nick-merrill
Copy link

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

@jiawenzhang
Copy link

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

@jaydeep82
Copy link

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

@chadpav
Copy link

chadpav commented Apr 21, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants