janakagamini
janakagamini

Reputation: 237

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:

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

Any help is much appreciated.

Upvotes: 5

Views: 551

Answers (1)

jiawen
jiawen

Reputation: 1208

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

    }
}

Upvotes: 1

Related Questions