ios - 为什么快照。值作为字典返回nil?

标签 ios swift firebase firebase-realtime-database firebase-authentication

我是Swift和Firebase的新手,我想知道为什么我会收到snapshot.value作为?即使用户已登录,NSDictionary仍为nil。

 let uid = Auth.auth().currentUser?.uid
        
        Database.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
            print(snapshot.value as? NSDictionary)
            if let value = snapshot.value as? NSDictionary{
                self.title = value["firstname"] as? String
                print("o")
            }
            print(self.title)
            
        }, withCancel: { (error) in
            print(error.localizedDescription)
        })
    }
//I want it to return the name of the user (Ben) as the title of the navigation bar, but instead it returns nil
数据库是:
    {
  "users" : {
    "-MBzED86cdVbvBNcYdpY" : {
      "email" : "bb@gmail.com",
      "firstname" : "benjamin",
      "username" : "bb"
    },
    "-MBzEWEQ3cX6d5aJOByR" : {
      "email" : "bslou.row@gmail.com",
      "firstname" : "bslou",
      "username" : "bbbslou.row"
    }
}
}
当我打印快照时,程序返回“Snap(mDNGl4fQXEMx1wPnjGuhILp5f8j1)”

最佳答案

数据库中“用户”下的子项不是Firebase Auth用户ID。它们看起来像随机生成的推送ID(又称“自动ID”)。使用UID的查询根本不会找到这些子级。请注意,快照的ID根本与数据库不匹配。快照快照为空-不包含任何数据。
您将不得不回到创建这些子代并使用用户的实际UID的代码,而不是使用自动ID。

关于ios - 为什么快照。值作为字典返回nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62921263/

相关文章:

ios - 如何在 CoreData iOS 中的实体中的同一索引处添加两个项目

swift - 打开应用程序时自动更新 UI

android - 如何在不引起客户端问题的情况下修改 Firebase 数据库结构(用于测试)

ios - 使用 Firebase 创建注册函数时 Swift 3 出错

javascript - NodeJS Firebase 应用程序在无效的 JSON 上崩溃

ios - 在 iOS Xcode 的 UIView 上移动图像

ios - Setter、Getter 和暴露真正的核心数据类型,我该怎么做?

ios - RxSwift 如何知道 TableView 重新加载的结束

ios - Metal Custom CIFilter 不同的返回值

ios - 状态栏问题上方的小线