规则条目重复Firebase无法正常工作 [英] Rule entries duplicates firebase not working

查看:40
本文介绍了规则条目重复Firebase无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android中有一个注册卖家的应用程序,卖家有一封独特的电子邮件,我将其存储在firebase中.创建一个规则,不允许添加重复项,但是它似乎不起作用.我在做什么错了?

I have an application in android that registers sellers, which have a unique email, I am storing them in firebase. Create a rule to not allow duplicates to be added but it does not seem to work. What am I doing wrong?

{
 "rules": {
   ".read": true,
   ".write": true,
   "sellers": {
     "$seller": {
       "email": {
         ".write": "!data.exists()"
       }
     }
   }
 }
}

我的添加方法

 public void addSeller(Seller seller){
    HashMap<String,Seller> map= new HashMap<>() ;
    String email = seller.getEmail().replace(".",",");
    map.put(email,seler);
    database.child("sellers").setValue(map);
}

推荐答案

您正在调用 push(),它会生成一个新的子级,据统计该子级是唯一的.

You're calling push(), which generates a new child that is statistically guaranteed to be unique.

如果要确保唯一的电子邮件地址,则必须保留一个集合,其中(已编码的)电子邮件地址为键:

If you want to ensure unique email addresses, you will have to keep a collection where the (encoded) email addresses are the keys:

emails
  pete@somedomain,com
  puf@somedomain,com

在这种结构下,以下规则将起作用,以确保电子邮件地址只能被写入一次:

With this structure, the following rule will work to ensure an email address can only be written once:

{
 "rules": {
   ".read": true,
   "emails": {
     "$email": {
       ".write": "!data.exists()"
     }
   }
 }
}

唯一值的主题经常出现,因此我建议您也查看以下内容:

The topic of unique values comes up regularly, so I recommend you also check out these:

  • Firebase android : make username unique
  • How do you prevent duplicate user properties in Firebase?
  • Enforcing unique usernames with Firebase simplelogin
  • unique property in Firebase
  • Firebase Unique Value
  • What Firebase rule will prevent duplicates in a collection based on other fields?

这篇关于规则条目重复Firebase无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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