【问题标题】:Swift + Firebase - Check if value of a node with an autoId existSwift + Firebase - 检查具有 autoId 的节点的值是否存在
【发布时间】:2017-12-05 16:53:45
【问题描述】:

我想检查用户名是否存在。

但是 Xcode 抱怨我的安全规则。

[Firebase/Database][I-RDB034028] 使用未指定的索引。考虑 将 /users 处的 ".indexOn": "profile/username" 添加到您的安全规则中 为了更好的性能

如果我删除“$uid”,Xcode 不再抱怨,但我的脚本不能更好地运行。

即使我输入存在于 firebase 中的用户名,快照也始终为空

我已经检查了论坛并尝试了来自不同用户的不同解决方案,但没有解决我的问题,所以也许我做错了什么?

这是我的规则

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "users": {
        "$uid": {
          "profile":{
            ".indexOn":["username"]
          }
        }
      }
  }
}

这是我的脚本

//check if username exist in the database
    static func checkIfUsernameExist(username: String, callback: @escaping (_ found: Bool) -> Void){
        print(username)
        let databaseRef = Database.database().reference(withPath: "users")
        databaseRef.queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            callback(snapshot.exists())
        })
    }

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    基于我有限的经验,我在databaseRef之后添加了.child

    databaseRef.child("users").queryOrdered(byChild: "profile/username").queryEqual(toValue: username).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot)
            callback(snapshot.exists())
        })
    

    至于 .indexOn,一旦您的数据变大,它将有所帮助。

    【讨论】:

    • 很高兴它可以帮助你@ItanHant
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多