Simple cloud code before Save gets pre-updated object

In a beforeSave hook, I want to get the state of an object before updating. In this particular case, this means that the user will not be able to change his choice after creating it. The pseudocode looks something like this:

 If (user has already voted) { deny; } else { accept; } 

And the code that I still have is:

 Parse.Cloud.beforeSave('votes', function(request, response) { if (!request.object.isNew()) { // This is an update. See if the user already voted if (request.object.get('choice') !== null) { response.error('Not allowed to change your choice once submitted'); } } response.success(); } 

But request.object is the state of the object with the update already applied.

Please note that the β€œvotes” object is created separately, so this does not allow you to insert, but not an update, it’s not enough. I need to know if a given field is set in the database.

+7
javascript
source share
5 answers

The request variable is an updated string. You can get the object identifier via request.object.id and use it to capture the current row from the database and check the current value, for example:

 Parse.Cloud.beforeSave('votes', function(request, response) { if (!request.object.isNew()) { var query = new Parse.Query("votes"); query.get(request.object.id, { // Gets row you're trying to update success: function(row) { if (row.get('choice') !== null) response.error('Not allowed to change your choice once submitted'); response.success(); // Only after we check for error do we call success }, error: function(row, error) { response.error(error.message); } }); } 
+8
source share

Although Krodmannix's answer is correct (and was useful to me), it does have the overhead of a full request. If you are doing something in beforeSave, you really want to optimize them. As a result, I find the fetch command much more preferable.

 Parse.Cloud.beforeSave('votes', function(request, response) { if (!request.object.isNew()) { var Votes = Parse.Object.extend("votes"); var oldVote = new Votes(); oldVote.set("objectId",request.object.id); oldVote.fetch({ success: function(oldVote) { if (oldVote('choice') !== null) { response.error('Not allowed to change your choice once submitted'); } else { response.success(); // Only after we check for error do we call success } }, error: function(oldVote, error) { response.error(error.message); } }); }); 
+14
source share

You can use Parse DirtyKeys to determine which field has been changed.

  Parse.Cloud.beforeSave(Parse.User, function(request, response) { for (dirtyKey in request.object.dirtyKeys()) { if (dirtyKey === "yourfieldname") { response.error("User is not allowed to modify " + dirtyKey); return; } } response.success(); }); 
+8
source share

If you are using a standalone Parse Server, there is a property on demand called "original" that is the object before the changes.

 Parse.Cloud.beforeSave("Post", function(request, response) { console.log(request.object); //contains changes console.log(request.original); //contains original response.success(); }); 
+4
source share

Job:

 var dirtyKeys = request.object.dirtyKeys(); var query = new Parse.Query("Question"); var clonedData = null; query.equalTo("objectId", request.object.id); query.find().then(function(data){ var clonedPatch = request.object.toJSON(); clonedData = data[0]; clonedData = clonedData.toJSON(); console.log("this is the data : ", clonedData, clonedPatch, dirtyKeys); response.success(); }).then(null, function(err){ console.log("the error is : ", err); }); 
0
source share

All Articles