ios - Firebase 快照在请求用户名时返回 null

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

我正在制作 Instagram 克隆,并希望获取用户名以供显示。信息在 firebase 的数据库部分,我试图通过快照来完成,但它总是返回。

fileprivate func fetchUser() {
    guard let uid = Auth.auth().currentUser?.uid else { return }


    Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot.value ?? "")
    }) { (err) in
        print("Failed to fetch user:", err)
    }
}

代码格式间隔很奇怪,但就是这样。我是不是没有让 Childs 正确,或者发生了什么事?

编辑:这里还有 Firebase Db 的快照。 Firebase screenshot

最佳答案

试试这个:

guard let uid = Auth.auth().currentUser?.uid else { return }
let reference = Database.database().reference()
reference.child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

    guard let dictionary = snapshot.value as? [String: Any] else { return }

    let username = dictionary["username"] as? String

    print(username ?? "")

}, withCancel: nil)

关于ios - Firebase 快照在请求用户名时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658312/

相关文章:

firebase - 如何从 firebase 检索 x 条目?

iphone - iOS In App Purchase 不退回所有产品

ios - 从应用程序 ios swift 中删除使用 UIImagePickerController 拍摄的所有本地镜像和视频

ios - 我可以使用低功耗蓝牙连接 iOS 和 Windows 吗?

ios - 用户遇到与 Firestore Pod 内部 grpc::ClientAsyncReaderWriter<grpc::ByteBuffer, grpc::ByteBuffer>::Finish 相关的崩溃

ios - 为什么在 AppDelegate (Swift) 中添加 iOS AdMobs GADBannerView 会导致 sigabrt?

ios - 未调用 TableViewCell 内的 UICollectionView

ios - Swift 等效于 Objective-C block ?

string - Swift 中的 toString 与 String

ios - GPUImage2 - 如何获取处理后的视频 URL 以保存在设备中?