Search code examples
firebasegoogle-cloud-firestorefirebase-security

Cloud Firestore security rules: how to check if property exists?


I need to check is property exists in document in store. allow update, delete: if resource.data.uid; in code above i have error "Property uid is undefined on object.", so how i can check is uid in data?


Solution

  • To check if the document contains a specific field, I use in:

    allow update, delete: if 'uid' in resource.data;
    

    As noted in the comments, if you want to check whether field is not in the data, you need to use parentheses:

    allow update, delete: if !('fieldname' in resource.data);