How to query objects in the CloudCode beforeSave?

前端 未结 4 1829
太阳男子
太阳男子 2021-01-23 16:52

I\'m trying to compare a new object with the original using CloudCode beforeSave function. I need to compare a field sent in t

4条回答
  •  长发绾君心
    2021-01-23 17:06

    Rather than performing a query, you can see the modified attributes by checking which keys are dirty, meaning they have been changed but not saved yet.

    The JS SDK includes dirtyKeys(), which returns the keys that have been changed. Try this out.

    var attributes = request.object.attributes; 
    var changedAttributes = new Array(); 
    
    for(var attribute in attributes) {
    
        if(object.dirty(attribute)) { 
            changedAttributes.push(attribute);
            // object.get(attribute) is changed and the key is pushed to the array
        }
    }
    

    For clarification, to get the original attribute's value, you will have to call get() to load those pre-save values. It should be noted that this will count as another API request.

提交回复
热议问题