【问题标题】:Swift, Firebase: No datas in realtime database after creating userSwift,Firebase:创建用户后实时数据库中没有数据
【发布时间】:2018-11-09 17:47:10
【问题描述】:

使用 Xcode 10.1、Swift 4.2 和 Firebase ##

使用以下代码将数据上传到 Firebase 后,我无法在实时数据库中看到数据:

static func createUser(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping () -> Void, onError: @escaping (_ error: String?) -> Void) {

    Auth.auth().createUser(withEmail: email, password: password) { (data, error) in
        if let err = error {
            onError(err.localizedDescription)
            return
        }
        // User erfolgreich erstellt
        guard let uid = data?.user.uid else { return }
        self.uploadUserData(uid: uid, username: username, email: email, imageData: imageData, onSuccess: onSuccess)
    }
}

static func uploadUserData(uid: String, username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void) {


    let storageRef = Storage.storage().reference().child("profile_image").child(uid)

    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        if error != nil {
            return
        }
    }

    storageRef.downloadURL(completion:  { (url, error) in
        if  error != nil {
            print(error!.localizedDescription)
            return
        }

        let profilImageURL = url?.absoluteString


        let ref = Database.database().reference().child("users").child(uid)
        ref.setValue(["username" : username, "email" : email, "profileImageURL": profilImageURL ?? "Kein Bild vorhanden"])

    })
     onSuccess()
}

firebase 中的设置:

应该以正确的方式工作。我已经查看了 firebase 文档,但没有找到更多信息。

【问题讨论】:

  • 你确定user has write permission/users/$uid吗?
  • 对不起我的错误。编辑了图片。读写为真。
  • 感谢分享。请始终将文本内容作为文本发布。文字图片几乎不能重复使用和搜索。
  • 所以你是说你的数据库在运行这段代码后仍然是空的?
  • 是的,在这段代码之后我的实时数据库是空的

标签: swift firebase firebase-realtime-database


【解决方案1】:

图片会上传到云存储吗?如果是这样但下载 URL 没有写入数据库,我猜这是因为在生成下载 URL 之前没有上传数据。由于上传函数是异步的,所以应该从闭包中调用downloadURL函数。

static func uploadUserData(uid: String, username: String, email: String, imageData: Data, onSuccess: @escaping () -> Void) {

    let storageRef = Storage.storage().reference().child("profile_image").child(uid)

    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        if error != nil {
            return
        }
        storageRef.downloadURL(completion:  { (url, error) in
        if  error != nil {
            print(error!.localizedDescription)
            return
        }
     onSuccess()
    }
}

【讨论】:

  • 是的,图片已上传到存储中。但不是用户数据。
  • 那你有没有尝试我的建议?
  • 已修复:我过早关闭了支架。非常感谢您的帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2021-07-26
  • 2016-11-03
  • 1970-01-01
  • 2021-06-10
  • 2019-07-31
相关资源
最近更新 更多