需要检查数组中值的Firebase规则(安全规则中的contains()) [英] Firebase rule that needs to check for value in array (contains() in security rules)

查看:84
本文介绍了需要检查数组中值的Firebase规则(安全规则中的contains())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Firebase规则内检查数组中的值.我正在尝试做这样的事情:

How do you check for a value in an array within a firebase rule. I am trying to do something like this:

  root.child('connections/'+auth.uid).child('friends').child(someFriendId).exists()

因此,在当前用户的连接节点下,如果friends数组中存在someFriendId,则允许访问. "someFriendId"不是friends数组的键,它是使用firebase push()方法自动生成的ID.

So under the connections node for current user, if someFriendId exists in the friends array, allow access. 'someFriendId' isn't the key for the friends array, that is an auto generated ID from using the firebase push() method.

推荐答案

通常,

In general, avoid arrays in distributed data, and read up on Arrays in Firebase in the guide on structuring data.

您不能在Firebase安全规则中执行包含".相反,您需要将用户存储为密钥,类似于我们将在安全文档中演示此用例.

You can't perform a "contains" in Firebase security rules. Instead, you'll want to store your users as the keys, similar to how we demonstrate this use case in the security docs.

因此,在您的示例中,唯一的更改是将数组索引替换为用户ID,并将其值更改为布尔值(或任何其他有用的值).然后,您的安全规则可以构造为:

So, given your example, the only change would be to replace the array indices with the user ids, and change the value to a boolean (or any other useful value). Then your security rules can be structured as:

root.child('connections/'+auth.uid+'/friends/'+someFriendId).exists();

这篇关于需要检查数组中值的Firebase规则(安全规则中的contains())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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