0

I am trying to use a beforeSave trigger on my Parse.com backend, but it looks like using that trigger causes the data sent up from my app to be ignored, instead saving only the changes I've made in the beforeSave trigger itself.

(Note: For the example below I'm using a slightly tweaked version of the Xamarin TodoParseAuth sample app.)

When using the following code:

Parse.Cloud.beforeSave("TodoItem", function(request, response) {
    request.object.set("Description", "Where have all my changes gone?");
    response.success();
});

Saving new objects causing ONLY the "Description" value to be saved, with the values actually sent to Parse from the iOS sample app to be ignored, saving the new row but saving NULLs into all other fields.

Log file shows:

I2015-10-05T01:30:12.365Z]v17 before_save triggered for TodoItem for user KiKpmdbErc:
Input: {"original":null,"update":{"ACL":{"KiKpmdbErc":{"read":true,"write":true}},"Description":"fifth notes","IsDone":true,"Title":"Fifth task"}}
Result: Update changed to {"Description":"Where have all my changes gone?"}

Attempting to update an existing row ignores the other updated values and also only updates the "Description" field (with the value from the beforeSave trigger). Log for an update shows:

I2015-10-05T01:52:14.267Z]v17 before_save triggered for TodoItem for user KiKpmdbErc:
Input: {"original":{"ACL":{"KiKpmdbErc":{"read":true,"write":true}},"Description":"This is my first task.  Yay.","IsDone":true,"IsNewest":false,"Title":"First Task","createdAt":"2015-09-18T23:08:16.159Z","objectId":"w0MLHAwgij","updatedAt":"2015-10-04T21:46:28.833Z"},"update":{"ACL":{"KiKpmdbErc":{"read":true,"write":true}},"Description":"This is my first task.","IsDone":false,"Title":"Still First Task"}}
Result: Update changed to {"Description":"Where have all my changes gone?"}

Both logs show the correct original: and update: values (showing the change I was attempting to make to Title, IsDone, and Description), but the only change actually written to the backend is the updated value I set in the beforeSave trigger. (The log file seems to be telling me this by the "Result: Update changed to ..." portion, but why?)

All the examples, other Stackoverflow questions, Google Groups postings, etc. I can find say this should be working correctly, so am I just missing something else or does something else need to be changed elsewhere in the code or my Parse application?

Update 10/9/2015:

Commenting out the request.object.set("Description"... line as shown below causes updates to be seemingly completely ignored and new rows written with all "user" fields set to "(undefined)", with nothing in the log or the error log.

Trigger:

Parse.Cloud.beforeSave("TodoItem", function(request, response) {
    //request.object.set("Description", "Where have all my changes gone?");
    response.success();
});

Log (this is the last entry in the log; no sign of the save):

I2015-10-09T17:49:15.350Z]Deployed v18 with triggers:
  TodoItem:
    before_save

Removing the beforeSave trigger completely restores "proper" behavior: updates and inserts go through properly.

Removal of trigger:

//Parse.Cloud.beforeSave("TodoItem", function(request, response) {
//    request.object.set("Description", "Where have all my changes gone?");
//    response.success();
//});

Log shows nothing after either updates or inserts (as expected, since there are now no triggers); last line in the log is the cloud code being updated:

I2015-10-09T18:00:46.686Z]Deployed v19 with triggers:

Putting back in the beforeSave trigger (with a different description) blocks the updates again:

Parse.Cloud.beforeSave("TodoItem", function(request, response) {
    request.object.set("Description", "Breaking me again are you?");
    response.success();
});

Only the description field was updated, but the log below shows that I attempted to update the Description, Title, and IsDone values:

I2015-10-09T18:13:15.559Z]v20 before_save triggered for TodoItem for user KiKpmdbErc:
  Input: {"original":{"ACL":{"KiKpmdbErc":{"read":true,"write":true}},"Description":"Breaking me again are you?","IsDone":false,"Title":"Fifth Task","createdAt":"2015-10-09T18:07:10.012Z","objectId":"yZmkTXZxte","updatedAt":"2015-10-09T18:09:01.602Z"},"update":{"ACL":{"KiKpmdbErc":{"read":true,"write":true}},"Description":"Breaking me again are you?  NOOOO","IsDone":true,"Title":"Fifth Task?"}}
  Result: Update changed to {"Description":"Breaking me again are you?"}

As far as I can see, the problem is definitely with the beforeSave trigger: adding one breaks the ability to correctly save new or changed data, but removing it restores correct saving.

BillB
  • 331
  • 3
  • 8
  • You've done good research here, but the thing that would make the case solid is a description of behavior (and log output to back it up) when (a) the `set("Description"` line is commented out, and/or (b) when the entire `beforeSave` is commented out. You might find that the object is *still* saving incompletely and that you're incorrectly assigning blame to beforeSave. – danh Oct 05 '15 at 05:24
  • Added additional tests & results above; definitely looks like the problem is just with the beforeSave. – BillB Oct 09 '15 at 18:22

1 Answers1

0

What version of Parse's SDK are you using? I had a similar problem with all my fields being gone suddenly and solved it by going back to using JavaScript SDK version 1.4.2.

Here is an answer where I described how to change it back to 1.4.2.

Community
  • 1
  • 1
eschanet
  • 1,063
  • 6
  • 15