Parse cloudcode beforeSave obtain pre-updated object

前端 未结 5 1952
独厮守ぢ
独厮守ぢ 2020-12-06 11:56

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

相关标签:
5条回答
  • 2020-12-06 12:34

    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();
    });
    
    0 讨论(0)
  • 2020-12-06 12:44

    The request variable is the updated row itself. You can get it's object id through request.object.idand 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);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-06 12:48

    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();
    });
    
    0 讨论(0)
  • 2020-12-06 12:51

    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);
            });
    
    0 讨论(0)
  • 2020-12-06 12:58

    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);
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题