1

Can you edit data in beforeSave callback and if yes, how?

Looking at various docs, it seem like this code should work (using "_User" or Parse.User) and I can see Ran beforeSave on a User and new user! in my logs but the key aaa is still empty..

I've tried to add user.save() after user.set but still no luck. Any idea?

Parse.Cloud.beforeSave("_User", function(request, response) {
  console.log('Ran beforeSave on a User');
  var user = request.object;
  if(user.existed()) {
    response.success(); 
  } else {
    console.log('new user!');
    user.set('aaa', 'xxx');
    response.success();
  }
});

EDIT 2015/12/03: Maybe critical info, this code is running on heroku. http://blog.parse.com/announcements/introducing-heroku-parse/

EDIT 2015/12/05: It seem to be a bug with Heroku / Parse integration. beforeSave hook are called but doesn't seem to allow you to edit the request.object..

EDIT 2015/12/07: I've created an issue on the CloudCode-Express repo https://github.com/ParsePlatform/CloudCode-Express/issues/6

4
  • You're trying to set value to user variable which is local. Try this : request.object.set('aaa','xxx'); Commented Dec 2, 2015 at 15:28
  • We're using the same user.set() function in our code and it works, even if using a local variable. We've had some problems with existed(), however, and for our use case we can go with if (!user.get('aaa')) {} instead. Commented Dec 2, 2015 at 17:31
  • I've tried by referencing the request.object directly before refactoring a bit.. no luck... Commented Dec 2, 2015 at 20:41
  • I'm having the same problem, also running beforeSave on heroku. I think it's a bug in the Parse webhook implementation, as it did work when this was implemented directly in Parse Cloud Code. Might have to put the beforeSave back on the Parse server and not heroku until they fix it... Commented Dec 4, 2015 at 20:48

2 Answers 2

1

Using Webhooks, you need to pass back the changed object:

response.success(object);

Sign up to request clarification or add additional context in comments.

1 Comment

Time to update the doc, parse.com/docs/cloudcode/… :)
0

Change your code to use isNew() method:

Parse.Cloud.beforeSave(Parse.User, function(request, response) {
  console.log('Ran beforeSave on a User');

  if(!request.object.isNew()) {
    response.success(); 
  } else {
    console.log('new user!');
    request.object.set('aaa', 'xxx');
    response.success();
  }
});

8 Comments

As I said, the log show new user! so the block is executed.. not sure it will give me more... but thx..
Is that the whole function ? What do you see in the log as Input and Result ?
yep.. I see 'Ran beforeSave on a User' && 'new user!'
can you post those two lines here ? the ones that start with Input and Result
2015-12-03T09:39:47.487912+00:00 app[web.1]: Ran beforeSave on a User 2015-12-03T09:39:47.487982+00:00 app[web.1]: new user!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.