Parse cloudcode beforeSave obtain pre-updated object

前端 未结 5 1972
独厮守ぢ
独厮守ぢ 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: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);
            }
        });
    });
    

提交回复
热议问题