实时数据库权限被拒绝 (swift)

Realtime Database permission denied (swift)

我想知道玩家是否已经在游戏中,但我对数据库编程完全陌生。我不断收到此错误

2022-02-07 17:32:51.787097+0100 Troeven2.0[64029:2674017] 7.10.0 - [Firebase/Database][I-RDB038012] / 的监听器(这里是正确!当前用户的用户 ID)失败:permission_denied

更新** 错误处理程序返回:

错误域=com.firebase代码=1“权限被拒绝”用户信息={NSLocalizedDescription=权限被拒绝}

**

我觉得这很奇怪,因为用户拥有我所看到的写入和读取自己数据的完全权利,规则:

{
  "rules": {
    "users": {
      ".read": "auth.uid != null",
      ".write": "auth.uid != null",
      "$uid": {
        ".write": "$uid == auth.uid"
      }
    }
  }
}

我的数据结构如下:

我用来访问的代码是这样的:

func checkIfInGame(completionHandler: @escaping (_ completionHandler: Bool) -> Void) {
    let currentUserID = Auth.auth().currentUser?.uid
    let ref = Database.database().reference().child(currentUserID!).child("gameInfo")
    
    ref.observe(.value , with: { (snapshot) in
        if let directory = snapshot.value as? [String: Any] {
            let gameID = directory["gameID"] as! String
            let isInGame = directory["inGame"] as! String
            var flag = false
            if isInGame == "yes" {
                flag = true
            }
            completionHandler(flag)
        }
    }) { (error) in
        print("error checking in game")
        completionHandler(false)
    }
}

知道我的代码立即打印错误“游戏中的错误检查”可能也很方便 非常感谢任何帮助和时间!

愚蠢的错误浪费了我的一天:我没有进入 child("users") 因此没有获得许可。我也切换到 getData 因为当被问到时我只需要一次数据....

func checkIfInGame(completionHandler: @escaping (_ completionHandler: Bool) -> Void) {
    guard let currentUserID = currentUser?.uid else {return}
    let ref = Database.database().reference().child("users").child(currentUserID)
    
    ref.child("gameInfo/inGame").getData(completion:  { error, snapshot in
        guard error == nil else {
            print(error!.localizedDescription)
            return;
        }
        let test = snapshot.value as? String ?? "no";
        
        print(test)
    })
}