2017-07-02 68 views
0

我想检查用户名是否存在。Swift + Firebase - 检查具有autoId的节点的值是否存在

但Xcode抱怨我的安全规则。

[Firebase/Database] [I-RDB034028]使用未指定的索引。考虑 加入“.indexOn”:‘个人资料/用户名’AT /用户您的安全规则 获得更好的性能

如果我删除‘$ UID’的Xcode不抱怨了,但我的脚本不工作得更好。

快照总是空,即使我进入一个存在于火力

用户名我已经检查了论坛,来自不同用户想尽了各种办法,但没有固定我的问题,所以也许我做错了什么?

这是我的规则

{ 
    "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()) 
     }) 
    } 

enter image description here

回答

0

根据我有限的经验,我databaseRef

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

至于.indexOn,一旦您的数据规模增大,它将会有所帮助。

相关问题