在Firebase中实施唯一属性的方法 [英] A way to enforce a unique property in Firebase

查看:67
本文介绍了在Firebase中实施唯一属性的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Firebase中,我有许多数据项,在这种情况下,它们是公告.每个公告都有一个子属性"ID",这对于每个公告都是唯一的.通过休息将公告发布到数据库时,是否可以强制ID唯一?也就是说,如果我尝试两次添加具有相同ID的公告,则第二个公告将失败.我怀疑我可以用规则做些什么,但是我不确定.

In my Firebase I have a number of items of data, in this case they are announcements. Each announcement has a child property of "ID", which is unique for every announcement. Is there a way I can enforce that the ID is unique when I post announcements to the database via rest? That is, if I tried to add an announcement with the same ID twice, the second one would fail. I suspect I can do something with rules, but I am not sure.

谢谢, Declan

Thanks, Declan

推荐答案

实现此目的的最佳方法是通过ID本身来标识每个公告,而不是将其作为子属性.然后,您可以使用以下安全规则:

The best way to achieve this would be to identify each announcement by the ID itself instead of having it be a child property. Then you can use a security rule like:

".write": "!data.exists()"

如果某人试图用已经存在的ID编写公告,则该写入规则将评估为false,因此将被禁止.

If someone tries to write an announcement with an ID that already exists the write rule will evaluate to false and therefore it will be disallowed.

您可以使用push()方法来生成按时间顺序排列的唯一标识符.

You can use the push() method to generate unique identifiers that are chronologically ordered.

这篇关于在Firebase中实施唯一属性的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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