Skip to content

"beforeSave" Cloud function causes an error during a POST command on Heroku. #650

Description

@grassland-curing-cfa

My Parse-server-example repo on GitHub was deployed to Heroku (NOT via the Heroku button) via deployment of my GitHub repo.

I used a cURL POST command line trying to add a new record in the GCUR_OBSERVATION class on my MongoLab hosted database,
curl -X POST -H "X-Parse-Application-Id: MY_APP_ID" -H "X-Parse-Master-Key: MY_MASTER_KEY" -H "Content-Type: application/json" -d "{\"ObservationStatus\":0}" https://{MY_APP}.herokuapp.com/parse/classes/GCUR_OBSERVATION

A beforeSave Cloud function was defined in the Cloud code file "main.js" for the GCUR_OBSERVATION class as below:

Parse.Cloud.beforeSave("GCUR_OBSERVATION", function(request, response) {
    console.log("*** beforeSave on GCUR_OBSERVATION called");
    Parse.Cloud.useMasterKey();
    sharedWithJurisArr = [];
    console.log("*** FLAG 1");
    if(!request.object.existed()) {
        console.log("*** FLAG 2");
        var sharedJurisSettingsQ = new Parse.Query("GCUR_SHARED_JURIS_SETTINGS");
        console.log("*** FLAG 21");
        sharedJurisSettingsQ.find().then(function(sjsObjs) {
            console.log("*** FLAG 22");
            for (var i = 0; i < sjsObjs.length; i ++) {
                var jurisdiction = sjsObjs[i].get("Jurisdiction");
                console.log("*** jurisdiction:" + jurisdiction);
                sharedWithJurisArr.push(jurisdiction);
            }
            console.log("*** FLAG 23");

            var sharedByArr = [];
            console.log("*** FLAG 24");

            for (var i = 0; i < sharedWithJurisArr.length; i ++) {
                sharedByArr.push({
                    "st" : sharedWithJurisArr[i],
                    "sh" : true
                });
            }
            console.log("*** FLAG 25");

            request.object.set("SharedBy", JSON.stringify(sharedByArr));
            console.log("*** FLAG 3");
            response.success();
        });
    } else {
        console.log("*** FLAG 4");
        response.success();
    }
});

Heroku logs showed the following messages after the cURL POST request was made:

2016-02-25T11:50:54.199259+00:00 app[web.1]: *** beforeSave on GCUR_OBSERVATION called

2016-02-25T11:50:54.199268+00:00 app[web.1]: *** FLAG 1

2016-02-25T11:50:54.199293+00:00 app[web.1]: *** FLAG 2

2016-02-25T11:50:54.199326+00:00 app[web.1]: *** FLAG 21

2016-02-25T11:51:24.195056+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/classes/GCUR_OBSERVATION" host=nemp-qld-test.herokuapp.com request_id=d517a443-f264-4adb-83db-f2c077c9da71 fwd="183.78.173.3" dyno=web.1 connect=0ms service=30001ms status=503 bytes=0service=30001ms status=503 bytes=0

I have tried the similar cURL POST command line to add a new record to any other class that does *_not *_have a beforeSave cloud function defined. But that worked.

So I am guessing it was the beforeSave function that caused the error.

Activity

  1. flovilmart commented on Feb 25, 2016

    @flovilmart
    Contributor

    Seems that your query is failing, we have some issues reported with querying in CloudCode that would not work properly like #356. Can you confirm by adding a .fail(function(err) {console.error(err); response.error(err)} to sharedJurisSettingsQ.find()

  2. gfosco commented on Feb 26, 2016

    @gfosco
    Contributor

    In the node environment, Parse.Cloud.useMasterKey() is not valid. You should pass the useMasterKey: true option to query calls and saves to use the master key.

    query.find({ useMasterKey: true }).then(...
    
    obj.save(null, { useMasterKey: true }).then(...
    

    Between this and adding the proper error handlers, I think you'll discover your issue. Please re-open or create a new issue if you have problems. Thanks.

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