Parse Server - afterSave push on specific field change

274 Views Asked by At

I have job collection and it has a field applicants which is Relation type of class User. Now I want to send a push notification to a user when an operation like AddRelation or RemoveRelation happens on the applicants field. Parse Object has following two methods available by which I can understand If i should send a push. these two methods are the following.

req.object.dirtyKeys()
req.object.op('applicants')

These two functions returns me expected result on beforeSave hook. I get an empty result in afterSave which is by documentation as expected.

Now my problem is I don't want to send push in beforeSave as there might be other validation logic comes up which eventually cancels the operation. I want send push only when the object is saved and the above condition met. Is there anything like to pass data from beforeSave to afterSave. Or am I missing something obvious?

1

There are 1 best solutions below

0
On

An easy -- although not too elegant -- way to pass data between beforeSave and afterSave is to set a property on the object being saved. When you save the object and don't want to pass any info, unset it; else set a value which can be interpreted by afterSave. Unfortunately you cannot easily clear the property in afterSave because this would cause a new save and thus trigger beforeSave again, so the rest of the application should ignore that property...