Firestore Security Rules - How can I check that a field is/isn't being modified?

前端 未结 9 622
孤城傲影
孤城傲影 2020-11-29 07:24

For the life of me, I cannot understand why the following is resulting in a false for allowing writes. Assume my users collection is empty to start

9条回答
  •  误落风尘
    2020-11-29 08:20

    The solution of Tom Bailey (https://stackoverflow.com/a/48177722/5727205) did look promising.

    But in my case I needed to prevent a field from being edited, and could have the case, that the field simply does not exist on the existing data. Thereby I added a check if the field exists.

    This solution does check two checks:

    1. If the field is not in request and non in the existing data (equals field not modified)
    2. or request and existing data are the same (equals field not modified)
     function isNotUpdatingField(fieldName) {
       return
         ( !(fieldName in request.resource.data) && !(fieldName in resource.data) ) || 
         request.resource.data[fieldName] == resource.data[fieldName];
    }
    

提交回复
热议问题