Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse query in cloud code not working after migrating to Heroku

I have migrated my Parse app to Heroku as per the migration guide on the Parse blog. Things seem to work fine except running a query in my cloud code. Here is my code:

Parse.Cloud.afterSave("Item", function(request) {
    //Parse.Cloud.useMasterKey(); //Uncomenting this line yields the same error

    var query = new Parse.Query(Parse.User);
    var prevAssigneeId = request.object.get("prevAssignee").id;
    var assigneeId = request.object.get("assignee").id;

    query.get(prevAssigneeId, { // <-- Results in an error
        success: function(prevAssignee) {
            console.log("Fetch prevAssignee: Success");
            query.get(assigneeId, {
                success: function(assignee) {
                    console.log("Fetch assignee: Success");

                    // Do something with the fetched users!
                },
                error: function(object, error) {
                   console.log("Query for assignee: ");
                   console.log(error);
                }
            });
        },
        error: function(object, error) {
            console.log("Query for prevAssignee: ");
            console.log(error); //<-- Error is logged here
        }
    });
});

Error

ParseError { code: undefined, message: 'unauthorized' }

This cloud code used to run fine when hosted on Parse. But after migrating I'm getting the above error. Other cloud code that does not use ParseQuery still work fine.

Any ideas on what the problem could be?

UPDATE

Still not able to find a fix for this so I tried changing the code to the following:

Parse.Cloud.afterSave("Item", function(request) {

    console.log("Executing afterSave");

    function findUser(user_id) {
        var query = new Parse.Query(Parse.User);
        //Parse.Cloud.userMasterKey();
        return query.get(user_id);
    }

    var prevAssigneePromise = findUser(request.object.get("prevAssignee").id);
    var assigneePromise = findUser(request.object.get("assignee").id);

    var promises = [prevAssigneePromise, assigneePromise];

    Parse.Promise.when(promises).then(function(prevAssignee, assignee) {

        //Do something here!
        console.log("This line was executed!");

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

Now I noticed the following behavior:

  • Run as is, it produces the same error: [ ParseError { code: undefined, message: 'unauthorized' }, ParseError { code: undefined, message: 'unauthorized' } ]
  • If I uncomment Parse.Cloud.userMasterKey();, there are no errors, but wont execute anything in the success block either i.e. log "This line was executed!". I can confirm that logging itself is working because other console.log(...) statements work.

Just FYI: Item is a Parse class with columns assignee and prevAssignee being columns that are pointers to _Users.

Any help is much appreciated.

like image 352
janakagamini Avatar asked Nov 09 '22 20:11

janakagamini


1 Answers

query.get(prevAssigneeId, {
    useMasterKey: true
    success: function(prevAssignee) {
        console.log("Fetch prevAssignee: Success");

    }
}
like image 53
jiawen Avatar answered Nov 14 '22 22:11

jiawen