Console / Debugging info for afterSave functions

How do I get print statements, or equivalent, when I call a Cloud Code afterSave function on a particular class?

The save request has, for example, the “grossScore” and “strokeIndex”.

What would be needed to print these values to the console? From here I could extend the logic.

The Parse Server is running on a Docker container running in AWS ECS - so I don’t easily have access to root filesystems, etc.

I may be misunderstanding your question, but do you mean console.log("grossScore: " + grossScore); ?

It probably is that simple, in 2 parts.

Where do I see console.log messages? in a container? on AWS ECS? CloudWatch?

And what syntax is required to access and use the saved item that kicked off this function call?

In Node.js, console.log will log to stdout, console.err will log to stderr. You would expect to see the output in your Node.js logs.

Where you write these logs to depends on your Node.js configuration and whether you can access them in AWS CloudWatch depends on your ECS configuration and whether you stream the logs into CloudWatch. If you use a container that is preconfigured (by AWS) for Node.js you could expect an AWS CloudWatch Log Group to be automatically created for you. You may want to take a look into the /logs directory in the container where parse server logs to by default and at the logging documentation section of Parse Server.

I haven’t specifically configured anything for CloudWatch in my config / deployment. Browsing around, I do not see any data about saving an item.

Using Portainer container (to manage other containers), I can view this container’s console log file. The latest entry is:

verbose: REQUEST for [POST] /parse/classes/Score: {,
“UserID”: {,
“__type”: “Pointer”,
“className”: “_User”,
“objectId”: “Eysu3hWTlj”,
},
“HoleNo”: 3,
“HoleSI”: 3,
“Club”: “MAGC”,
“HolePar”: 4,
“CourseName”: “Lakes”,
“ScoreGross”: 4,
“ScoreHcap”: 13,
} {“method”:“POST”,“url”:“/parse/classes/Score”,“headers”:{“host”:“parse-host.com:1337”,“content-type”:“application/json; charset=utf-8”,“accept”:“/”,“x-parse-session-token”:“token-code”,“x-parse-application-id”:“appID”,“x-parse-client-key”:“masterKey”,“x-parse-installation-id”:“install-id”,“x-parse-os-version”:“13.2.2 (18G5033)”,“accept-language”:“en-us”,“accept-encoding”:“gzip, deflate”,“x-parse-client-version”:“i1.17.3”,“content-length”:“174”,“user-agent”:“Parse-2/1 CFNetwork/1120 Darwin/18.7.0”,“connection”:“keep-alive”,“x-parse-app-build-version”:“1”,“x-parse-app-display-version”:“1.0”},“body”:{“UserID”:{“__type”:“Pointer”,“className”:“_User”,“objectId”:“Eysu3hWTlj”},“HoleNo”:3,“HoleSI”:3,“Club”:“MAGC”,“HolePar”:4,“CourseName”:“Lakes”,“ScoreGross”:4,“ScoreHcap”:13}},
verbose: RESPONSE from [POST] /parse/classes/Score: {,
“status”: 201,
“response”: {,
“objectId”: “VAiUe7yF4e”,
“createdAt”: “2020-07-05T20:54:48.501Z”,
},
“location”: “http://parse-host.com:1337/parse/classes/Score/VAiUe7yF4e”,
} {“result”:{“status”:201,“response”:{“objectId”:“VAiUe7yF4e”,“createdAt”:“2020-07-05T20:54:48.501Z”},“location”:“http://parse-host.com:1337/parse/classes/Score/VAiUe7yF4e”}},

But I do not see the afterSave console.log entry. This is initially set just to print the value of the HoleNo just saved:

Parse.Cloud.afterSave("Score", function(request, response) {
     
     newScore = request.object;
     console.log("Hole: " + newScore["HoleNo"]);

}

After making changes to the Cloud Code .js files, do I need to restart the server / redeploy the containers, to take effect?

So I did restart the container, and I can see my debug line of code in the console log - so I know my Cloud Code is being executed.

However, from this, I was expecting the whole object and it’s properties to be printed out in the console log…

Parse.Cloud.afterSave("Score", function(request, response) {
 
 newScore = request.object;
 console.log("Print the object: " + newScore);
});

The console log contents:

verbose: REQUEST for [POST] /parse/classes/Score: {,
“UserID”: {,
“__type”: “Pointer”,
“className”: “_User”,
“objectId”: “Eysu3hWTlj”,
},
“HoleNo”: 3,
“HoleSI”: 3,
“Club”: “MAGC”,
“HolePar”: 4,
“CourseName”: “Lakes”,
“ScoreGross”: 4,
“ScoreHcap”: 13,
} {“method”:“POST”,“url”:“/parse/classes/Score”,“headers”:{“host”:“my-parse-host.com:1337”,“content-type”:“application/json; charset=utf-8”,“accept”:“/”,“x-parse-session-token”:“token_key”,“x-parse-application-id”:“auction-house”,“x-parse-client-key”:“themasterkey”,“x-parse-installation-id”:“3509ccc2-0552-4d3c-9b62-a9f4c14c7f77”,“x-parse-os-version”:“13.2.2 (18G5033)”,“accept-language”:“en-us”,“accept-encoding”:“gzip, deflate”,“x-parse-client-version”:“i1.17.3”,“content-length”:“174”,“user-agent”:“Parse-2/1 CFNetwork/1120 Darwin/18.7.0”,“connection”:“keep-alive”,“x-parse-app-build-version”:“1”,“x-parse-app-display-version”:“1.0”},“body”:{“UserID”:{“__type”:“Pointer”,“className”:“_User”,“objectId”:“Eysu3hWTlj”},“HoleNo”:3,“HoleSI”:3,“Club”:“MAGC”,“HolePar”:4,“CourseName”:“Lakes”,“ScoreGross”:4,“ScoreHcap”:13}},
Print the object: [object Object],
info: afterSave triggered for Score for user Eysu3hWTlj:,
Input: {“UserID”:{“__type”:“Pointer”,“className”:“_User”,“objectId”:“Eysu3hWTlj”},“HoleNo”:3,“HoleSI”:3,“Club”:“MAGC”,“HolePar”:4,“CourseName”:“Lakes”,“ScoreGross”:4,“ScoreHcap”:13,“createdAt”:“2020-07-06T09:50:16.595Z”,“updatedAt”:“2020-07-06T09:50:16.595Z”,“objectId”:“8BPBInRGSO”} {“className”:“Score”,“triggerType”:“afterSave”,“user”:“Eysu3hWTlj”},
info: afterSave triggered for Score for user Eysu3hWTlj:,
Input: {“UserID”:{“__type”:“Pointer”,“className”:“_User”,“objectId”:“Eysu3hWTlj”},“HoleNo”:3,“HoleSI”:3,“Club”:“MAGC”,“HolePar”:4,“CourseName”:“Lakes”,“ScoreGross”:4,“ScoreHcap”:13,“createdAt”:“2020-07-06T09:50:16.595Z”,“updatedAt”:“2020-07-06T09:50:16.595Z”,“objectId”:“8BPBInRGSO”},
Result: undefined {“className”:“Score”,“triggerType”:“afterSave”,“user”:“Eysu3hWTlj”},
verbose: RESPONSE from [POST] /parse/classes/Score: {,
“status”: 201,
“response”: {,
“objectId”: “8BPBInRGSO”,
“createdAt”: “2020-07-06T09:50:16.595Z”,
},
“location”: “http://my-parse-host.com:1337/parse/classes/Score/8BPBInRGSO”,
} {“result”:{“status”:201,“response”:{“objectId”:“8BPBInRGSO”,“createdAt”:“2020-07-06T09:50:16.595Z”},“location”:“http://my-parse-host.com:1337/parse/classes/Score/8BPBInRGSO”}},

All I have is:

Print the object: [object Object]

If I change to:
console.log("Print the object: " + newScore["HoleNo"]);

It returns:

Print the object: undefined

You can try to use:

  • console.log("obj: ", obj);
  • console.log("obj: " + JSON.stringify(newScore));
  • console.log("obj: " + JSON.stringify(newScore, null, 4)); to beautify

You may encounter some pitfalls when trying to log certain objects with their properties (such as circular structures), but these are Node.js / Javascript fundamentals that are unrelated to Parse Server, so luckily you can find a ton of existing advice online if you do an online search.

Yes, that is what I was looking for, and then how to grab the properties. From trial & error, this is of the format:

newScore = request.object;
property1 = newScore.get("property1");

Simple when you know how / see it…