在Cloud Firestore规则中-如何检查键是否为空 [英] In Cloud Firestore rules - How do I check if a key is null

查看:103
本文介绍了在Cloud Firestore规则中-如何检查键是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cloud Firestore规则中-我有一个名为task的文档,我想查看某些数据(assignee字段)是否为空/不存在.

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.

我尝试过:

  1. resource.data.assignee == null-不起作用(错误)
  2. !resource.data.hasAll(['assignee'])-不起作用(错误)
  1. resource.data.assignee == null - Does not work (Error)
  2. !resource.data.hasAll(['assignee']) - Does not work (Error)

从文档中可以看出-这确实造成了错误: // Error, key doesn't exist allow read: if resource.data.nonExistentKey == 'value';

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

推荐答案

阅读Firestore安全规则文档的列表比较

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']);

request.resource.data是包含字段和值的映射.为了使用hasAll,我们必须首先获取键作为值列表,如

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'])

这篇关于在Cloud Firestore规则中-如何检查键是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆