【问题标题】:How to set the Firebase: Realtime Database Rules to only let Unique names Childs如何设置 Firebase:实时数据库规则只允许唯一名称 Childs
【发布时间】:2022-02-17 07:08:16
【问题描述】:

所以我正在开发一个用户可以使用用户名创建帐户的项目。这个用户名应该是唯一的,我必须在 Firebase 实时数据库中设置规则。我对设置这些感到非常困惑。我找到了一些关于此的帖子,但没有奏效,这是我目前的规则:

 {
  "rules": {
   "users": {
  "$uid": {
    ".write": "auth !== null && auth.uid === $uid",
    ".read": "auth !== null && auth.provider === 'password'",
  }
},
  "usernames" : {
    ".write" : "true",
      ".read" : "true",
        ".validate" : "!root.child(\"username\").exists()"
    
  }

  }
}

这是数据模型: I made a screenshot, how it looks in the database

希望有人能帮帮我!

【问题讨论】:

    标签: json database firebase firebase-realtime-database


    【解决方案1】:

    将您的 usernames 规则更改为:

    "usernames" : {
        "$username": {
            ".validate" : "!root.child('usernames').hasChild($username)"
        }
    }
    

    【讨论】:

    • 更简单并且做同样的事情:!data.exists()。换句话说:验证当前的$username 还不存在。
    猜你喜欢
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2020-09-20
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多