In Cloud Firestore rules - How do I check if a key is null

雨燕双飞 提交于 2020-05-25 12:12:45

问题


In Cloud Firestore Rules - I have a document called task and I want to see if some data (assignee field) is null / don't exists.

I've tried:

  1. resource.data.assignee == null - Does not work (Error)
  2. !resource.data.hasAll(['assignee']) - Does not work (Error)

From the documentation - it states that this indeed creates an error: // Error, key doesn't exist allow read: if resource.data.nonExistentKey == 'value';


回答1:


Reading the list comparisons of the Firestore Security rules documentation here, we can see that hasAll returns true if all values are present in the list.

// Allow read if one list has all items in the other list
allow read: if ['username', 'age'].hasAll(['username', 'age']);

The request.resource.data is a map containing the fields and values. In order to use hasAll, we must first get the keys as a list of values as shown here.

!resource.data.keys().hasAll(['assignee'])



回答2:


Looking at the docs - https://firebase.google.com/docs/reference/rules/rules.Map

k in x  - Check if key k exists in map x

so this should work (without the keys())

!('assignee' in resource.data) 



回答3:


if you want to make sure a key is null you need to check that this key is not part of the resource keys property:
!resource.data.keys().hasAny(['assignee'])

you can also use hasAll or hasOnly. more info here



来源:https://stackoverflow.com/questions/46630728/in-cloud-firestore-rules-how-do-i-check-if-a-key-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!