In a beforeSave
hook I want to obtain the state of the object prior to the update. In this particular case it is to stop a user from changing their choice once
If you are using the self hosted Parse Server, there is a property on request called "original" that is the object before changes.
Parse.Cloud.beforeSave("Post", function(request, response) {
console.log(request.object); //contains changes
console.log(request.original); //contains original
response.success();
});
The request
variable is the updated row itself. You can get it's object id through request.object.id
and use this to grab the current row from the database and check the current value, like so:
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);
}
});
}
You can use Parse DirtyKeys to identify which field has 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();
});
This Worked :
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);
});
While Krodmannix's response is correct (and was helpful to me) it has the overhead of a full query. If you are doing things in beforeSave, you really want to streamline them. As a result, I believe a fetch command is much 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);
}
});
});