Parse cloudcode beforeSave obtain pre-updated object

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

提交回复
热议问题