1

I'm trying to make use of the afterSave hook in Parse.

However even a simple print out of the ID from the object that was just saved does not show in logs, and if statements do not pass to allow other methods to be performed. The relevant code:

Parse.Cloud.afterSave(Parse.User, function(request) {
  Parse.Cloud.useMasterKey();
  var user = request.object;
  console.log('After save for user ID: ', user.id);
  if (!user.existed() && user.get('isTrainer')) {
  // does not enter here

The log that prints out has no ID, just the string I typed. I also tried printing out the request object but the Parse logging system doesn't seem capable of printing out objects which is unfortunate. It also doesn't enter the if statement because using get also returns no information.

Anyone have an idea of why this might be?

For completeness here is the object so you can see its being saved correctly:

v31 after_save triggered for _User for user PlVMz2zCfC:
  Input: {"object":{"createdAt":"2015-12-24T21:01:01.678Z","email":"yui@bb.bb","firstName":"Pppp","isTrainer":true,"lastName":"Pop","objectId":"PlVMz2zCfC","phone":"(555) 555-5555","trainer":{"__type":"Relation","className":"Trainer"},"updatedAt":"2015-12-24T21:01:01.678Z","username":"yui@bb.bb"}}
  Result: Success
Scott Fister
  • 1,213
  • 12
  • 24

1 Answers1

0

Since this is an afterSave trigger on the User class, you should use request.user instead of request.object

I find it helpful to output everything to the console during development to help with debugging.

Parse.Cloud.afterSave(Parse.User, function(request) {
    Parse.Cloud.useMasterKey();
    var user = request.user;

    console.log("user = " + JSON.stringify(user));
    console.log("user.id = " + JSON.stringify(user.id));
    console.log("user.existed() = " + JSON.stringify(user.existed()));
    console.log("user.get("isTrainer") = " + JSON.stringify(user.get("isTrainer")));

    if (!user.existed() && user.get("isTrainer")) {
        ...
Russell
  • 3,099
  • 2
  • 14
  • 18
  • Unfortunately this has the same result: no ID is printed in the console log, and it does not enter the `if` statement. – Scott Fister Dec 25 '15 at 02:25
  • Try using `console.log("request.user = " + JSON.stringify(request.user));` and let me know if anything shows up. – Russell Dec 25 '15 at 02:32
  • It does! `request.user = {"createdAt":"2015-12-25T02:34:19.005Z","email":"ff@bb.nn","firstName":"Gggg","isTrainer":true,"lastName":"Ffgg","phone":"(555) 555-5555","sessionToken":"xx","trainer":{"__type":"Relation","className":"Trainer"},"updatedAt":"2015-12-25T02:34:19.005Z","username":"ff@bb.nn","objectId":"yzQGLpBuC5"}`. So that's a great way to see an object output. Why do the `get`s and `.id` notational not work though? – Scott Fister Dec 25 '15 at 02:35
  • See my updated answer and try to output each of the statements individually to help trace what's going on. As to why a `get` or `id` may not work can vary. Sometimes it could be due to ACLs or some of the special security precautions that Parse has built into the `User` class. Regardless, those shouldn't matter since you're using the master key – Russell Dec 25 '15 at 02:47
  • The result: `user = {"createdAt":"2015-12-25T02:49:24.109Z","email":"gg@hh.bbt","firstName":"Hhgg","isTrainer":true,"lastName":"Ffff","phone":"(555) 555-5555","sessionToken":"8uy3Ta6vh1FMQQWKYPgA8wTWe","trainer":{"__type":"Relation","className":"Trainer"},"updatedAt":"2015-12-25T02:49:24.109Z","username":"gg@hh.bbt","objectId":"QwX47B0TuC"} user.id = "QwX47B0TuC" user.existed() = true user.get(isTrainer) =` Strangely `existed()` returns `true`, but this is a new user. – Scott Fister Dec 25 '15 at 02:50
  • Has your question been answered? :) – Russell Dec 25 '15 at 04:02